Home » Community » Forums » For Beginners » GetAsyncKeyState()
  Intel sponsors gamedev.net search:   
[Control Panel] [Register] [Bookmarks] [Who's Online] [Active Topics] [Stats] [FAQ] [Search]

Add Forum to Favorites |  Send Topic To a Friend | View Forum FAQ | Track this topic


 Last Thread Next Thread 
 GetAsyncKeyState()
Post New Topic  Post Reply 
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.

 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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.

 User Rating: 1386   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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.

 User Rating: 1019   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

Also, here is reference if you don't have it:

MSDN: GetAsyncKeyState

 User Rating: 1019   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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.

 User Rating: 1386   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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.

 User Rating: 1019   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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

 User Rating: 1386   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

All times are ET (US)

Post Reply
 Last Thread Next Thread 
Forum Rules:
You may not post new threads
You may not post replies
You may not edit your posts
You may not use HTML in your posts
Jump To:
Administrative Options: