Jump to content
  • Advertisement

Archived

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

Haytil

DirectInput Question- Holding down keys

This topic is 5237 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

Hi, I''m reading in the keyboard state when using DirectInput. My question is, is there any way to configure DirectInput (or the keyboard device) so that a key is not recognized as being pressed until it has been released and re-pressed? I don''t want my player to be able to hold down the CTRL key and have a repeated affect: I want the program to ignore the effect of the CTRL key until it is released and pressed once again. Thanks -Gauvir_Mucca

Share this post


Link to post
Share on other sites
Advertisement
You can handle that pretty easily... this is how i do it in my progs:


#define KEYDOWN(name, key) ((name)[(key)] & 0x80)

void KeyboardInput(IDirectInputDevice8* pKeyboard)
{
static char LastKeyboardState[256];
char KeyboardState[256];
pKeyboard->GetDeviceState(sizeof(KeyboardState),(LPVOID)&KeyboardState);
if(KEYDOWN(KeyboardState, DIK_I) && !KEYDOWN(LastKeyboardState, DIK_I))
{
// Your code here;
}
memcpy(LastKeyboardState, KeyboardState, 256*sizeof(char));
}


Pretty straight forward... just keep a copy of the last keyboard state in a static variable or global variable, then whenever a key is down just check if it was down last frame or not... if it was down then you know the key has been held down, if it was up then you know it was only just pressed during this frame.

Share this post


Link to post
Share on other sites
I know it''s pretty trivial to optimize this on modern day processors, but to save a little "time", you can keep a pointer on which array you are currently working on and switch it at every call, instead of copying the content.


#define KEYDOWN(name, key) ((name)[(key)] & 0x80)
void KeyboardInput(IDirectInputDevice8* pKeyboard)
{
static char KeyboardState[256][2];
static char Current = 0;
char Previous = (Current == 0)?1:0;
pKeyboard->GetDeviceState(256,(LPVOID)&KeyboardState[Current]);
if(KEYDOWN(KeyboardState[Current], DIK_I) && !KEYDOWN(KeyboardState[Previous], DIK_I))
{
// Your code here;

}
Current = (Current == 0)?1:0;
}


Or something like that, I edited on the previous code. It''s untested, but it shows what I mean.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!