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

DirectInput Question- Holding down keys

Started by Haytil Apr 16, 2004 at 12:47 PM 2 replies 950+ views
Original Post
Haytil
Haytil
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
SpaceDude
SpaceDude
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.
Haytil
Haytil
Thanks for your help!
persil
persil
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.

Topic Locked

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

Sign in to reply to this topic.