Skip to main content
gamedev.net
🔒 Locked

Using GetAsyncKeyState

Started by kburkhart84 Sep 27, 2005 at 9:01 PM 7 replies 3.8k views
Original Post
kburkhart84
kburkhart84
OK. Now I'm trying to use this function, but it returns a short, the most significant bit and least significant bit are two different pieces of information. I need to access both bits of information, so how can I split the two bits. For example, for a DWORD, you can use the HIWORD and LOWORD macros, and for the WORD type, the HIBYTE and LOBYTE macros. What can I use for the short variable type, or what would be a method for it? Thanks in advance.


Mercury
Mercury
A word is an unsigned short, so you can just use HIBYTE and LOBYTE, as far as I know. I've never used it, though.
kburkhart84
kburkhart84
The short returned by the function is as far as I know a signed short. Do you think I would run into any problems doing that. I'll try it anyway though. Thanks.


Mercury
Mercury
Quote:
Original post by kburkhart84
The short returned by the function is as far as I know a signed short. Do you think I would run into any problems doing that. I'll try it anyway though. Thanks.

I'm pretty sure it would work, but if you have to cast the data to unsigned I think its layout in memory would change and hence become useless. I'm not really sure, I try to keep my bit fiddling to a minimum. [smile]
kburkhart84
kburkhart84
OK. Using the HIBYTE works fine, I guess, though I'm not sure if I even had to use it because it worked using the value itself. But the LOBYTE doesn't work. I even tried converting the value to signed but that doesn't work either. I have seen this before to get the most significant bit.
GetAsyncKeyState(Key) & 0x8000


This line returns true or false correctly for the most significant bit, but I have no idea what the value would be for the least significant bit. I'm not very good with hex numbers yet, though I can barely read binary, I can't do hex at all.


muelland
muelland
Maybe I'm misunderstanding the problem here, but if you have a short and want to get the least significant bit, couldn't you just use the and bit operator?

short data = GetAsyncKeyState(key); // however you get the data into your shortshort lsb_mask = 0x0001;short msb_mask = 0x8000;if (data & lsb_mask == lsb_mask){   // least significant bit is 1}else{   // least significant bit is 0}if (data & msb_mask == msb_mask){   // most significant bit is 1}else{   // most significant bit is 0}


0x0001 is hex for the binary number 0000 0001 (I added the space for clarity).
0x8000 is hex for the binary number 1000 0000

The code isn't very succinct but I wanted to try to be as clear as possible.
I hope this points you in the right direction.
Dave Hunt
Dave Hunt
I'm pretty sure kburkhart84 meant byte when he said bit.
JD
JD
if(GetAsyncKeyState(VK_BLAH) < 0)
// key was pressed

remember it's signed short which means the high order bit is negative. The low order bit is positive 1.
kburkhart84
kburkhart84
I found a solution. The high bit part always works, but I can't get the low part to work. Look at this. It works perfectly.
for(int loop=0;loop<256;loop++)	{		keys[loop]=((GetAsyncKeyState(loop)&0x8000)==0x8000);		if(newkeys2[loop]==false)		{			newkeys2[loop]=true;			newkeys[loop]=true;		}		if(!((GetAsyncKeyState(loop)&0x8000)==0x8000))		{			keys[loop]=false;			newkeys[loop]=false;			newkeys2[loop]=false;		}				}


This is run once per frame and updates all the keys at once. Keys array is if the key is down. newkeys is if it is newly down. newkeys2 is if it is newly down, but only changes when the key goes back up. When you check if the key is newdown, it changes it to false in the newkey array so that it is only true once, but since in the newkeys2 array it is true until the key goes back up, it works. I don't know how to explain it well. I have a KeyDown function and a NewKeyDown function. In the New one, it makes the newkeys array[Key] false so it only returns true once. I have tested and it works just like it should. No NewKey repeats until you actually lift the key, which is what the newkeys2 is for.

**should I post this as a snippet article??


Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.