what does the 0x80 code mean when referring to the keyboard controls in C++ Windows environment?
For example,
if(GetKeyState('K') & 0x80) {
//do something
}
Thanks everyone!
0x stands for hexadecimal, and 80 is just 128 in decimal.
I understand that “0x8000” means 32,768 shown as a HEX number.
Update
A flurry of downvotes propelled me into investigating this further. Here's how the return values (in hex) of GetKeyState works. I don't quite get the toggle property of a key like k but I'm assuming there's some default state it toggles from.
0 Default State, key up
ff80 Default state, key down
1 Toggled, key up
ff81 Toggled, key down
So 0xff80 is added whenever the high-order bit needs to be set and the low-order bit makes sense. So now we know why the 0x80 approach works --- since the high-order bit of the lower byte is set as well!
Old Answer
GetKeyState returns a SHORT where if the high-order bit is 1 it means the key is up. The bitwise AND operation with 0x80 just checks if that bit is 1 since in binary 0x80 is 10000000.
Therefore the statement GetKeyState('K') & 0x80 would return 0x80 if the high-order bit of the value returned by GetKeyState('K') is 1 and 0 if the high-order bit is 0.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With