Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 486
Paz Avatar asked Apr 30 '10 18:04

Paz


People also ask

What is 0x80 C++?

0x stands for hexadecimal, and 80 is just 128 in decimal.

What does 0x8000 mean?

I understand that “0x8000” means 32,768 shown as a HEX number.


1 Answers

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.

like image 64
Jacob Avatar answered Oct 05 '22 22:10

Jacob