What does the 0x80 code mean when referring to keyboard controls

Paz picture Paz · Apr 30, 2010 · Viewed 10.2k times · Source

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!

Answer

Jacob picture Jacob · Apr 30, 2010

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.