Question

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!

No correct solution

OTHER TIPS

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.

The MSDN documentation of the function states:

If the high-order bit is 1, the key is down; otherwise, it is up.

bit-wise and with 0x80 gives you the high order bit, the if checks if the result is zero or non-zero and in essence checks the value of that bit.

This check however looks like a mistake since GetKeyState() returns a SHORT and to check the high order bit of a short you need to bit-wise and with 0x8000.
So I suggest you check the return value with a debugger and verify how this works in reality.

I think you mean 0x8000, not 0x80. If that is the case, you should consult the documentation (http://msdn.microsoft.com/en-us/library/ms646301(VS.85).aspx) which has the following to say on the return value of GetKeyState:-

The return value specifies the status of the specified virtual key, as follows:

•If the high-order bit is 1, the key is down; otherwise, it is up. •If the low-order bit is 1, the key is toggled. A key, such as the CAPS LOCK key, is toggled if it is turned on. The key is off and untoggled if the low-order bit is 0. A toggle key's indicator light (if any) on the keyboard will be on when the key is toggled, and off when the key is untoggled.

0x80 doesn't mean anything as far as I know though

According to the documentation

The return value specifies the status of the specified virtual key:
If the high-order bit is 1, the key is down; otherwise, it is up.
If the low-order bit is 1, the key is toggled. A key, such as the CAPS LOCK key, is toggled if it is turned on. The key is off and untoggled if the low-order bit is 0. A toggle key's indicator light (if any) on the keyboard will be on when the key is toggled, and off when the key is untoggled.

Perhaps with a non-toggleable key (such a 'K'), the low-order (ambiguous term - perhaps they mean 0x0080 ?) and high-order (0x8000) bits do the same thing?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top