Archived

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

handling keyboard input

This topic is 5087 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 have an app thats using some GetASyncKeyState() but I really just want to know when single key is pressed.. with that function it checks the key''s state.. which results in an action getting executed more times than I''d like. Whats a solution that allows me to know the diffence between repeat keystrokes (result of holding the key down) and key presses? (down and up)

Share this post


Link to post
Share on other sites
You could implement it with two variables, one to hold the current state of the key and one to hold the last known state of the key.

void DoSomethingWhenUpArrowIsFirstPressed()
{
static BOOL bUpArrowPressedLastUpdate = FALSE;
BOOL bUpArrowIsPressedNow = bUpArrowIsPressed(); /* Use GetASyncKeyState() in this function to find out the current state of the up arrow key and return TRUE if it is pressed */

if((bUpArrowPressedLastUpdate == FALSE) && (bUpArrowIsPressedNow == TRUE))
{
// Do whatever needs to be done when up arrow key goes from up state to down state
}

bUpArrowPressedLastUpdate = bUpArrowIsPressedNow;
}

Of course, you should probably create an input object class that tracks all the keys you are interested in and whether they are pressed for the first time or being held down or just got let up. Just use a big array for the keys with two bools for each key, one for current state and one for state last update. Comparing between the two states tells you whether a key just got pressed, whether it is still being pressed or whether it just got let up.

If you want to handle repeat keys, just not at the rate that they can be fed to you by the system, use a timer. Every time that you signal that a key is "still pressed" set the timer for however often you want repeats to be signaled and don't signal another repeat until the timer expires.

[edited by - Igilima on January 7, 2004 6:19:09 PM]

Share this post


Link to post
Share on other sites