🔒 Locked
Using GetAsyncKeyState
Advertisement
Original Post
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.
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.
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]
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.
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.
GetAsyncKeyState(Key) & 0x8000This 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.
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?
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.
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.
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.
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??
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.
Advertisement