Jump to content
  • Advertisement

Archived

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

BtySgtMajor

Keyboard Handling

This topic is 5667 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

So, in my main "game loop", I use DirectInput to read the status of all keys. I then do a check of the high bits of the state of the specific key I want to check to see if it''s down (the states are stored in an array). eg. if (keyboardState[DIK_F1] & 0x80) { ... } ...now, say I have code inside that block that toggles a map being displayed/not displayed. When I press that key, the map "flickers", ie. that bit of code is being run multiple times per keypress, presumably because the game loop is happening so fast, and each time through it reads the key as "down". How can I get around this? Thanks for the help!

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster

This is what I''d do.

// Put these somewhere an initialize them to false.
static bool isF1Down;
static bool WasF1Released;

if( KeyDown(DIK_F1) )
{
if( !isF1Down )
isF1Down = true;
}

if( KeyUp(DIK_F1) )
{
if( isF1Down )
{
WasF1Released = true;
}
else
{
WasF1Released = false;
}

if( isF1Down )
isF1Down = false;

}

now make some functions

inline bool IsF1Released() { return f1WasReleased; }
inline bool IsF1Down() { return (isF1Down != false); }
inline bool IsF1Up() { return (isF1Down == false; }

now call these functions in your main game loop this should make it so those boolean variables are only modified when an actual change happens.

Share this post


Link to post
Share on other sites
Hmm, ok, but that seems like an awful lot of code to just check that, especially if I do that for all the keys. Also, to check for "KeyUp", would I need to check the low bits? Or what? Thanks for the help, btw!

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!