|
-
January 18th, 2011, 09:44 PM
#1
How to use GetKeyState()
Code:
WORD keyState;
//.............
keyState = GetKeyState(VK_SHIFT);
if(HIWORD(keyState) & 0x80)
{
MessageBox(NULL, TEXT("SHIFT Down"), TEXT("Key State"), MB_OK);
}
Not sure how to go about detecting whether the hi order bit is 1...which is what I need D:
-edit-
ok nm I got it
Code:
if(GetKeyState(VK_SHIFT) & 0x80)
{
MessageBox(NULL, TEXT("SHIFT Down"), TEXT("Key State"), MB_OK);
}
Last edited by Amaz1ng; January 18th, 2011 at 09:57 PM.
-
January 19th, 2011, 04:01 AM
#2
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).
-
January 19th, 2011, 10:16 AM
#3
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... 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.
Last edited by Eri523; January 19th, 2011 at 10:23 AM.
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
-
January 20th, 2011, 09:58 AM
#4
Re: How to use GetKeyState()
 Originally Posted by Eri523
EDIT: Oh, wait...  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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|