Archived

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

DirectInput- How to know when a key is released?

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I use this code: ReadDevice(g_pDIDeviceKeyboard, (void*)KeyStateBuffer, 256); if(KeyStateBuffer[DIK_SPACE] & 0x80) //Do something if(KeyStateBuffer[DIK_RETURN] & 0x80) //Do something But how could I know when the key is released ?? ------------------------- Account: Hedos - Level 1 Programmer

Share this post


Link to post
Share on other sites
I ran into a similar problem.

You need a second key state buffer called something like KeyStateLastFrameBuffer[256]

Change the code you posted to be something like

//Save state from last frame
for(int i = 0; i < 256; i++)
KeyStateLastFrameBuffer = KeyStateBuffer[i];

//Get current state
ReadDevice(g_pDIDeviceKeyboard, (void*)KeyStateBuffer, 256);


if((KeyStateBuffer[DIK_SPACE] & 0x80) && !(KeyStateLastFrameBuffer[DIK_SPACE] & 0x80))
//Do something for key down for the first time
//(avoids those nasty key is constantly down errors that cause things like instant scrolling to the top and bottom of menues)


if(KeyStateBuffer[DIK_SPACE] & 0x80)
//Do something for a key down
//Will constantly act on this every frame as long as the key is held down.
//Will also act on this even if key down for first time was acted on unless you put it in the else of the preceding if

if(!(KeyStateBuffer[DIK_SPACE] & 0x80) && (KeyStateLastFrameBuffer[DIK_SPACE] & 0x80))
//Do something for key up after it was pressed last frame

Those are your three different events
Keydown, KeyPressed (or KeyStillDown) and KeyUp

Share this post


Link to post
Share on other sites