But why does this work? I mean the -32767 |
-32767 in hex is 0x8001 (as a 16-bit signed integer)
This means both
(x & 0x8000)
and (x & 0x0001)
will be true.
This means:
- The real-time state of the key is being held down (x & 0x8000)
- The key has just transitioned from released to pressed (x & 0x0001)
Again note that it is a bad idea to compare directly with ==.. since some other bits may be set. If for example, they decide to change functionality to make bit 1 return something else regarding the key state... GetAsyncKeyState might return 0x8003. In which case comparing to -32767 will fail, but the &0x8000 and/or &0x0001 checks will still work just fine.
And it doesnt make copies of the key if i want to store it. |
I don't understand what you mean by this. GetAsyncKeyState doesn't make copies of anything... it just tells you the state of the key.
But if i use the 0x8000 it gets the keys to fast. |
The 0x8000 bit of GetAsyncKeyState gives you the
real time state of the key. This means that
at the moment you call the function, the bit will be set if the key is currently being held down.... or it will be clear if the key is not being held down.
So if you are spinning in a loop which calls GetAsyncKeyState 1000 times per second for a single key... and the user presses the key for only half a second... this means GetAsyncKeyState will return the high bit set for 500 calls.
If you just want to know whether or not the user just pressed the key for the first time (as opposed to seeing if it's down at this exact moment), you have a few options:
1) Use bit 0 instead of bit 15 (ie: & 0x0001). But again note this can be unreliable as other programs on the machine might interfere with the keystate reported.
2) Keep your own copy of the previous state so that you can see when it transitioned from up to down. Something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
// at some larger scope:
bool was_pressed = false;
bool IsKeyPressed()
{
bool is_pressed = (GetAsyncKeyState(x) & 0x8000) != 0;
// is_pressed will be true if the key is currently being held down
if(!was_pressed && is_pressed) // if it wasn't down before, but is down now, they
{ // just pressed it
was_pressed = is_pressed;
return true;
}
// otherwise, it either is not held down, or it was already down... so it was not just
// pressed
was_pressed = is_pressed;
return false;
}
|
or 3) Don't use GetAsyncKeyState, and instead use some other mechanism for getting the key press. IE: catch and process the WM_KEYDOWN message.