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

10,846

Solution 1

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.

Solution 2

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.

Solution 3

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

Solution 4

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?

Share:
10,846

Related videos on Youtube

Paz
Author by

Paz

Updated on April 20, 2022

Comments

  • Paz
    Paz about 2 years

    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!

    • Stewart
      Stewart about 14 years
      What do you mean by "working"? If you don't know what it is supposed to do, how do you know it is working?
    • Paz
      Paz about 14 years
      @Jacob, yeh that's what i was thinking, strangly it was working.
    • user1703401
      user1703401 about 14 years
      This is typical for the Windows API. Lots of programmers got this wrong (it is a crummy API), so they fixed the API without documenting it and also turn on bit 7. A better test is GetKeyState('K') < 0.
  • Stewart
    Stewart about 14 years
    The MSDN documentation is not ambigous - the high order bit of a SHORT is 0x8000. The low order bit is 0x0001. I know this to be true because when ever I have used this function I usually test it with <0 rather than &0x8000 - it achieves the same thing.
  • BlueRaja - Danny Pflughoeft
    BlueRaja - Danny Pflughoeft about 14 years
    @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..
  • Stewart
    Stewart about 14 years
    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
    Stewart about 14 years
    It will work, but you are depending on undocumented behaviour. 0x8000 is better.