Ignoring held down keys

Started by
5 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
I''m not too keen on DirectInput, so there might be a different solution, but maybe you can check if the keystate is inactive right after the first if statement, such as:

if ( !keystate[ DIK_KEYID ] & 0x80 )

and then have it do something. But it might be too sudden for the user to release the key.
Errr... no that can''t work. Usually, when you press a key normally, it is held down for a few frames. I only wan''t to capture the event at the instance of the key being pushed down, not after it is held. (Ex. pressing space to shoot, but holding down space doesn''t cause you to shoot continously)
No, Canadians DON'T all live in igloos. They live in one BIG igloo!
Errr... no that can''t work. Usually, when you press a key normally, it is held down for a few frames. I only wan''t to capture the event at the instance of the key being pushed down, not after it is held. (Ex. pressing space to shoot, but holding down space doesn''t cause you to shoot continously)
No, Canadians DON'T all live in igloos. They live in one BIG igloo!
Errr... no that can''t work. Usually, when you press a key normally, it is held down for a few frames. I only wan''t to capture the event at the instance of the key being pushed down, not after it is held. (Ex. pressing space to shoot, but holding down space doesn''t cause you to shoot continously)
No, Canadians DON'T all live in igloos. They live in one BIG igloo!
you''ll have to store another flag somewhere to tell if the key was pressed last frame already... i.e. have another array parallel to your keystate one, and check that ("keyalreadydown" is a boolean array here):
if((keystate[DIK_KEYID] & 0x80) && keyalreadydown[DIK_KEYID])  {//do the stuff} 



--- krez (krezisback@aol.com)
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
// RIGHT arrow
// Just know that RightKeyDown has to remain when the function ends. i.e ( make it static)
if( keystate[DIK_KEYID] & 0x80 )
{
if( m_RightKeyDown == false )
{
RightKeyDown = true;
//Add code here
}
}
else
RightKeyDown = false;

This topic is closed to new replies.

Advertisement