Archived

This topic is now archived and is closed to further replies.

Shadowflare

Trouble with DirectInput

Recommended Posts

I am using DirectInput in an application, and I am have some difficulty. I am using Microsoft''s KEYDOWN macro (#define KEYDOWN(name, key) (name[key] & 0x80)) to check to see if a key is pressed. I want to perform an action when a specific key is pressed, however, the action takes place regardless of what key I press. What is going wrong? Thank you for your help :-D

Share this post


Link to post
Share on other sites
It is impossible to figure out this problem with out seeing code.

TO use the KEYDOWN macro, you must use it after a successful call to GetDeviceState. Make sure that this call succeedes:

char buffer[256];
m_spDIKeyboard->GetDeviceState(sizeof(buffer),(LPVOID)&buffer);

If this doesn''t succeed, then buffer will have garbage in it. Alot of times this will fail because of the device not being acquired.

Now you should be able to use the KEYDOWN macro.

Good luck...
/jk

Share this post


Link to post
Share on other sites
Here is the code for my handling function:

void HandleInput()
{
char buffer[256];
HRESULT hr;

hr = g_lpDIDevice->GetDeviceState(sizeof(buffer),(LPVOID)&buffer);

if(FAILED(hr))
{
if(hr == DIERR_INPUTLOST)
{
hr = g_lpDIDevice->Acquire();

if(FAILED(hr))
MessageBox(g_hWnd, "Failed to get state of keyboard. Input device lost.", NULL, NULL);

} else {
MessageBox(g_hWnd, "Failed to get state of keyboard. Reason Unknown.", NULL, NULL);
}
}

if(KEYDOWN(buffer, DIK_RIGHT))
{
MessageBox(g_hWnd, "Right", NULL, NULL);
}

}

If you have trouble reading it due to indentation, I'll explain it. But I do not get any of the message boxes I have set up in case of errors, and no matter what key I press, I get the "Right" message box.



[edited by - ShadowFlare on October 6, 2003 5:35:15 PM]

Share this post


Link to post
Share on other sites
I think I have/had the exact same problem as you (I have a post on here about my DirectInput problem as well). I used to get gibberish in the buffer so that it acted as if every key was pressed. In fact, when I used ZeroMemory(256, &buffer) , then it would zero the buffer such that all inputs are defaulted to zero.. but GetDeviceState never seems to fill the buffer correctly. Try ZeroMemory to see if it makes it so that no keys will respond... In any case, I''m not sure what the problem is, for myself as well.

Share this post


Link to post
Share on other sites
You are trying to read the buffer even when you know that the keyboard failed. Try this...

void HandleInput()
{
char buffer[256];
HRESULT hr;

hr = g_lpDIDevice->GetDeviceState(sizeof(buffer),(LPVOID)&buffer);

if(FAILED(hr))
{
if(hr == DIERR_INPUTLOST)
{
hr = g_lpDIDevice->Acquire();

if(FAILED(hr))
{
MessageBox(g_hWnd, "Failed to get state of keyboard. Input device lost.", NULL, NULL);
return;
}
} else {
MessageBox(g_hWnd, "Failed to get state of keyboard. Reason Unknown.", NULL, NULL);
return;
}
}
if(KEYDOWN(buffer, DIK_RIGHT))
{
MessageBox(g_hWnd, "Right", NULL, NULL);
}
}

Share this post


Link to post
Share on other sites
That''s still badly structured...it won''t work probably. Try this:

void HandleInput()
{
char buffer[256];
HRESULT hr;

hr = g_lpDIDevice->GetDeviceState(sizeof(buffer),(LPVOID)&buffer);

if(FAILED(hr))
{
if(hr == DIERR_INPUTLOST)
hr = g_lpDIDevice->Acquire();
return; // want to return because contents of buffer are not valid!

}
if(KEYDOWN(buffer, DIK_RIGHT))
{
MessageBox(g_hWnd, "Right", NULL, NULL);
}
}


See if THAT works

Share this post


Link to post
Share on other sites