Ignoring held down keys

Started by
2 comments, last by Fundy Glostna 22 years, 1 month ago
I''ve acquired a keyboard device with DirectInput, and every frame I use GetDeviceState and save the keyboard state in an array. I can detect if a key is pressed easily, using
  
if(keystate[DIK_KEYID] & 0x80) {//do the stuff}

  
But I only want the program to "do the stuff" if the specified key has been just been pressed, in the instantaneous frame, and is not being held down.
No, Canadians DON'T all live in igloos. They live in one BIG igloo!
Advertisement

You can program in a key memory. Set up an array of booleans the same length as your keystate buffer. When you detect a keydown, set the keydown[that_key] to true, when you detect that it isn''t down, set it back to false.

This means scanning the entire key array every time. Alternately, buffered mode could provide what you need.


Stay Casual,

Ken
Drunken Hyena
Stay Casual,KenDrunken Hyena
i''m not really sure what your problem is, but i think the stuff is done by pressing any key. that''s right ?? to answer the question it would be helpfull to know if you use buffered dxinput or not.
for buffered you normaly use a DIDEVICEOBJECTDATA DIOd. this is a array of a given size.
now your running throught the array, looking in the DIOd.dwData if there is your specific key. for example DIK_ESCAPE. if there is you know the state of this key has changed. perhaps the key is up or down now. if the DIOd.dwData & 0x80 is TRUE the key was pressed else the keys up.

for immediate date it''s nearly the same

i hope this is some help for you
you''ve got your array of 256 characters for the keyboard which
is the current state THIS frame.

at the beginning of every frame make a copy of the array that held the state for the previous frame.

lets call the array that holds the current state
currstate and teh previuos state in prevstate.

then do a copy from the currstate to prevstate
memcpy(prevstate, currstate, sizeof(currstate));// prevstate now holds the keyboard data from last frame// get the data into currstate for the current frame // (do this here - I can''t remember the code...)// now compare the 2 - if the key is down this frame// but wasn''t down the previous frame then we know it// is being hit for the first time and isn''t being held downif ( (currstate[DIK_WHATEVER] & 0x80) && !(prevstate[DIK_WHATEVER] & 0x80) ){// key is being pressed} 


hope thats what you need


Gobsmacked - by Toby Murray

This topic is closed to new replies.

Advertisement