Re: How to use GetKeyState()
The return value of GetKeyState is a short what normally is a 16-bit integer. Hence, the short which has a bit combination where only the highest bit is set is '1000000000000000' binary or 0x8000 hex. The samples often use macro HIWORD which extracts the high 8 bits of the short, i. e. '10000000' binary or 0x80 hex. By doing a binary & operation on the return value with that you can test whether the bit was set cause 'AND' would only return non-zero when both operands have the same bit position set to 1 for at least 1 bit.
So
Code:
if ((keyState & 0x8000) != 0)
or
Code:
if ((HIWORD(keyState) & 0x80) != 0)
both are testing if the highest bit was set in keyState (which must be a short).
Re: How to use GetKeyState()
Sorry, but GetKeyState() returns a SHORT (16-bit) and HIWORD() returns the uppermost 16 bits of a DWORD (i.e. 32-bit value). I didn't actually look at the definition of that macro but I would expect that applying HIWORD() to the SHORT returned by GetKeyState() would return 0 in all cases.
If necessary at all (here itsmeandnobodyelse is right), the applicable macro here would be HIBYTE().
EDIT: Oh, wait... :o If, wehn passed to the HIWORD() macro, the signed 16-bit SHORT would be converted to an unsigned 32-bit DWORD using sign extension, using HIWORD() here actually would give the correct result, though achieved in an uncorrect way.
Re: How to use GetKeyState()
Quote:
Originally Posted by
Eri523
EDIT: Oh, wait... :o If, wehn passed to the HIWORD() macro, the signed 16-bit SHORT would be converted to an unsigned 32-bit DWORD using sign extension, using HIWORD() here actually would give the correct result, though achieved in an uncorrect way.
Double Nope.
Keystate is here declared as a WORD (even though GetKeyState() returns a short), so it won't get sign extended into a DWORD. It'll be zero-extended.
If keystate would have been declared as a signed short, the example wouldn't work properly. It would sign-extend, but in that case HIWORD(shortVal) will be 0xFFFF.
Due to compiler optimisations, the HIBYTE(keyState)&0x80 will be slightly more optimal.