|
||||||||||||||||||
Add Forum to Favorites | Send Topic To a Friend | View Forum FAQ | Track this topic |
Last Thread Next Thread ![]() |
| GetAsyncKeyState() |
|
![]() mike003 Member since: 8/2/2005 From: Cary, NC, United States |
||||
|
|
||||
| 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. |
||||
|
||||
![]() TDragon Member since: 5/24/2002 From: La junta, CO, United States |
||||
|
|
||||
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. |
||||
|
||||
![]() JoshM Member since: 7/15/2005 From: Dallas, TX, United States |
||||
|
|
||||
| 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. |
||||
|
||||
![]() JoshM Member since: 7/15/2005 From: Dallas, TX, United States |
||||
|
|
||||
| Also, here is reference if you don't have it: MSDN: GetAsyncKeyState |
||||
|
||||
![]() TDragon Member since: 5/24/2002 From: La junta, CO, United States |
||||
|
|
||||
| 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. |
||||
|
||||
![]() JoshM Member since: 7/15/2005 From: Dallas, TX, United States |
||||
|
|
||||
| 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. |
||||
|
||||
![]() TDragon Member since: 5/24/2002 From: La junta, CO, United States |
||||
|
|
||||
| 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 |
||||
|
||||
All times are ET (US)![]() |
Last Thread Next Thread ![]() |
|