Skip to main content
GameDev.net gamedev.net
🔒 Locked

Direct Input Problem

Started by hamxxx Nov 29, 2001 at 2:45 PM 2 replies 200+ views
Original Post
hamxxx
hamxxx
Im having a small problem with getting usefull data from the keyboard. Basically i have initialised Direct Input and the keyboard. The code below shows the routine to collect the data from keyboard and I have created a message box to output the corresponding key identifier when pressed. Examples of this data is "0xc8" or "0x01" obviously depending on the key that is pressed. My question is how do I compare this data stored in strElement(e.g. If(strElement == "0xc8") This is what i want to do but there is a problem with data types. Any help greatly appreciated. HRESULT KeyboardInput(HWND hDlg) { HRESULT hr; TCHAR strElement[10]; BYTE diks[256]; // DirectInput keyboard state buffer int i; if( NULL == lpDIDEVICE8 ) return S_OK; ZeroMemory( &diks, sizeof(diks) ); hr = lpDIDEVICE8->GetDeviceState( sizeof(diks), &diks ); for( i = 0; i < 256; i++ ) { if( diks & 0x80 ) { wsprintf( strElement, TEXT("0x%02x "), i ); MessageBox(hDlg, strElement,"Key Pressed",MB_OK); } } return S_OK; }
NightShade
NightShade
Hiya.

I''m not really sure what you mean, but I dont see why you have to try to compare the string values, as they are nothing more than the value "i", so just use that.

How I usually handle it (when thinking of immediate key states, buffered is a little different) is to use the DIK_XXX values. For example to determine if the "C" key is pressed, once per loop you would update your Keys[] array (like you are doing).

Then later in the code, when you want to see if the C key is pressed you do something like
if ((Keys[DIK_C] & 0x80) != 0)
{
// if it gets here, C is pressed.
}

This works okay. The only problem is if the user presses the key really fast, and your GetDeviceState() call "misses" the keypress. This gets worse the slower your loop is. You can fix it by using buffered keyboard input.

Anyway I hope this might help get you some ideas.

-ns-
hamxxx
hamxxx
Thanks for response Nightshade, I am a little drunk now but will have a go tomorrow.

Thanks Again
Biggles
Biggles
But programming when drunk is fun! (And gives interesting results as well!)


--------------------

Never eat anything bigger than your own head.
--------------------Never eat anything bigger than your own head.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.