Declare Function GetKeyState Lib "user32.dll" (ByVal nVirtKey As Long) As Integer
GetKeyState returns the current status of one of the keys on the keyboard. This status contains two pieces of information: the key's toggle state and the key's pressed state. The information is put into the return value. The toggle state is analogous to the toggle nature of the Caps Lock, Num Lock, and Scroll Lock keys, but Windows records toggle information about every key. The toggle information is stored in bit &H80 of the return value. The pressed state is true if the key is currently being depressed. The pressed information is stored in bit &H01 of the return value. See the example for more information on how to use the return value.
Example:
' Read the status of the Enter key
x = GetKeyState(&H0D) ' Enter's virtual key code is &H0D
If (x And &H80) = &H80 Then ' check to see if it is toggled
Debug.Print "Enter is currently toggled."
End If
If (x And &H01) = &H01 Then ' check to see if it is depressed
Debug.Print "Enter is currently depressed."
End If
Related Call: GetKeyboardState
Category: Keyboard
Back to the index.