Public Group

# GetAsyncKeyState()

This topic is 5180 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 on other sites
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 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 on other sites
Also, here is reference if you don't have it:

MSDN: GetAsyncKeyState

##### 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 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 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

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 16
• 11
• 9
• 24
• 52