|(I have no idea where you pulled 0x8000 and 0x0001 from)|
It's basic binary:
GetKeyState returns a 'SHORT' which is 16-bits wide. This means the highest bit.. or the "most significant bit" (MSB) is bit 15, and the lowest/least significant bit is bit 0.
To calculate the value of a specific bit, you do (1<<n), where 'n' is the bit number (ie: 0 for LSB, and 15 for MSB in this case).
(1<<15) = 32768 = 0x8000 (hex)
(1<<0) = 1 = 0x0001 (hex)
|However, shouldn't my if statement work as well despite not being a bit check?|
Not necessarily. GetKeyState only guarantees the state of bits 0 and 15. The other bits could have completely arbitrary/random values. Just because it works on your machine doesn't mean it will work everywhere.
|Is there a downfall to my check versus the bit check?|
Yes. You could potentially fail to acknowledge keypresses that happen. For example, GetKeyState may return -4. Since -4 has the MSB set and the LSB clear, this would indicate the key is down, but not toggled. However your code would not detect it as such.
-4 is also a perfectly legal value for the function to return. Just because you don't get it on your machine for this key doesn't mean it's the same for everyone.
|it was still odd that it was stepping in to the if statement when by itself without keys being pressed. Is this the drawback of not using bits or is something else awry? |
It's hard to say. I find it doubtful that it was caused by the & issue.... since if anything your code would fail to detect actual keypresses -- not detect keypresses that never happened.
What might be happening is GetKeyState buffering weirdness. Like you pressed F8 at one point but didn't process the WM_KEYUP event, so GetKeyState still thinks F8 is pressed even though the user released it a long time ago. Using GetAsyncKeyState
instead of GetKeyState
would solve that issue if that was indeed the problem.
Further reading on the difference between the two can be found here: