Jump to content
  • Advertisement
Sign in to follow this  
mike003

GetAsyncKeyState()

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

How would I use GetAsyncKeyState() in my code? I tried just subtituting it in for getchar() (which I was originally using, but i dont like the pause), but that wont work. Pls help, becuz its getting reeeeally annoying.

Share this post


Link to post
Share on other sites
Advertisement
GetAsyncKeyState() is normally used as a part of a game loop, for games that update themselves constantly. Use it to detect whether a key is being pressed or not. For example:

if (GetAsyncKeyState(VK_LEFT))
{
MoveMyCharacterLeft();
}
else if (GetAsyncKeyState(VK_RIGHT))
{
MoveMyCharacterRight();
}

VK_LEFT and VK_RIGHT are the Windows API virtual keycodes that correspond to the left and right arrows on the keyboard.

This is extremely simplified, including not taking into account the entire return value of GetAsyncKeyState() -- but you probably don't need much more than that.

Share this post


Link to post
Share on other sites
GetAsyncKeyState simply tells you whether a given key is pressed down at the instant you call the function. The return value can't be trusted or used as TDragon suggested, I believe, since it will return a value with the highest bit set if the key is down, and *sometimes* will return a value with the lowest bit set if the key was pressed after the previous call to GetAsyncKeyState. But you can't trust that lowest bit because of multitasking.

I believe you should do:

if( GetAsyncKeyState( VK_LEFT ) & 0x8000 )

To determine if a key is down at that instant. If you're trying to get typed input (like somebody typing in their name) then you shouldn't use GetAsyncKeyState.

Share this post


Link to post
Share on other sites
As the MSDN documentation states, the lowest bit action can't be relied upon because sometimes it will not be set. However, the highest bit will always be set if the key is pressed at the time of calling, and sometimes the lowest bit will be set when the key was pressed since the last call, so the function can be relied on for a game loop in the way my code does.

Share this post


Link to post
Share on other sites
I've had various problems when not explicitly testing the high bit - sometimes the low bit would be set for a while when the high wasn't (i.e. the key gets "stuck" when it shouldn't). It may have been because I was debugging or something like that, but either way I recommend you just save yourself a potential headache and test the only useful bit.

Your mileage may (and evidently does) vary :) Just speaking from my own personal experience. The code is easier to read when not testing the bits so if it works for you then go with it.

Share this post


Link to post
Share on other sites
Hm. Well, I rely on portable solutions provided by libraries such as Allegro and SDL the majority of the time anyway, so I haven't had much of an opportunity to test GetAsyncKeyState() to the limit. I do know that it (a simple boolean check on the return value) is what the makers of one 3d engine I tested out recommended (although the engine wasn't the greatest and I dropped it quite soon after). Anyways, happy coding to the OP. :D

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!