#### Archived

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

# Trouble with DirectInput

This topic is 5248 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 on other sites
Show the code that processes your key press.

##### 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 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 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 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 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 on other sites
Thank you all for your help. Zeroing the buffer before using it seemed to be the key. I appreciate your responses!