Archived

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

Ignoring held down keys

This topic is 5775 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''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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
// 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;

Share this post


Link to post
Share on other sites