I figured out GetKeyState with relative ease. But I think I need GetAsyncKeyState, too and I can't figure out what the return values mean. I think that if the value is less than 0, then the key is being pressed when the call is being made, but other than that I'm lost. Can anyone help?
Comments
Windows 95: Windows 95 does not support the left- and right-distinguishing constants. If you call GetAsyncKeyState with these constants, the return value is zero.
Remarks
The GetAsyncKeyState function works with mouse buttons. However, it checks on the state of the physical mouse buttons, not on the logical mouse buttons that the physical buttons are mapped to. For example, the call GetAsyncKeyState(VK_LBUTTON) always returns the state of the left physical mouse button, regardless of whether it is mapped to the left or right logical mouse button. You can determine the system's current mapping of physical mouse buttons to logical mouse buttons by calling
GetSystemMetrics(SM_SWAPBUTTON)
which returns TRUE if the mouse buttons have been swapped.
You can use the virtual-key code constants VK_SHIFT, VK_CONTROL, and VK_MENU as values for the vKey parameter. This gives the state of the shift, ctrl, or alt keys without distinguishing between left and right.
Windows NT: You can use the following virtual-key code constants as values for vKey to distinguish between the left and right instances of those keys.
Code Meaning
VK_LSHIFT VK_RSHIFT
VK_LCONTROL VK_RCONTROL
VK_LMENU VK_RMENU
These left- and right-distinguishing constants are only available when you call the GetKeyboardState, SetKeyboardState, GetAsyncKeyState, GetKeyState, and MapVirtualKey functions.
Thanks, but...
The least significant bit in the return value for GetAsyncKeyState changes while the key is held down.
This is the code I used to try and figure this out(it is in a timer event):
Static KeyState As Integer
Temp = GetAsyncKeyState(65)
If Temp <> KeyState Then
KeyState = Temp
Text1.SelText = KeyState & Chr$(9) & GetKeyState(65) & vbNewLine
End If
This is a list of return values:
-32767 -128
0 0
-32767 -127
-32768 -127
0 1
-32767 -128
0 0
-32767 -127
-32768 -127
0 1
-32767 -128
-32768 -128
0 0
-32767 -127
-32768 -127
0 1
-32767 -128
-32768 -128
-32767 -128
-32768 -128
-32767 -128
1 0
0 0
The toggle of the low-order bit is obvious in both calls. In GetKeyState, it changes when the key is released. In GetAsyncKeyState, it changes while the key is down. The "0" is obvious here, it indicates the key has been released. The neg. values indicate the key is being held. Where does the "1" come from? Why does the low-order toggle while the key is held? Does it indicate the repeat? If it does, then why does it toggle so fast when the repeat is so slow? I can use GetKeyState to monitor keys while my app has the focus, but apparently I need GetAsyncKeyState to monitor at other times.
Thanks again to anyone who can help!