views:

319

answers:

4

Hi, 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!

+1  A: 

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?

BlueRaja - Danny Pflughoeft
Stewart
@Stewart: That is what I would think too - but then, why in the world would this work (if it does indeed work)? ...Also, what you stated has nothing to do with whether the "low-order bit" is 0x0080 or 0x0001..
BlueRaja - Danny Pflughoeft
I don't think I've ever had a need for the low bit. I suspect that the code "works" because the documentation says nothing about the meaning of bits 1 through 14. For a depressed non-togglable key 0xfffe would be a perfectly value return value within the meaning of the docs
Stewart
+2  A: 

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.

Jacob
However, the high order bit of a `SHORT` would be `0x8000`
James McNellis
That would be the high bit of a BYTE, but the function returns SHORT, which is 16-bits
Stewart
Updated my answer with the solution!
Jacob
+3  A: 

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.

shoosh
+1  A: 

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

Stewart
`0x80` will work, check my answer.
Jacob
It will work, but you are depending on undocumented behaviour. 0x8000 is better.
Stewart