GetKeyState Function

Declare Function GetKeyState Lib "user32.dll" (ByVal nVirtKey As Long) As Integer

Platforms

Description & Usage

GetKeyState determines the current status of a key. The function both finds whether the key is currently pressed down or not, and determines if the key is currently toggled. The keyboard information retrieved by this function is thread-specific; its information does not necessarily reflect key states pertaining to the system as a whole.

Return Value

If the &H1 bit of the return value is set, the key is toggled. If the &H80 bit of the return value is set, the key is currently pressed down.

Visual Basic-Specific Issues

None.

Parameters

nVirtKey
The virtual-key code of the key to read the status of. Windows NT, 2000: This could also be one of the following flags which distinguish between the left and right Ctrl, Alt, and Shift keys:
VK_LSHIFT
The left Shift key.
VK_RSHIFT
The right Shift key.
VK_LCONTROL
The left Ctrl key.
VK_RCONTROL
The right Ctrl key.
VK_LMENU
The left Alt key.
VK_RMENU
The right Alt key.

Constant Definitions

Const VK_LSHIFT = &HA0
Const VK_RSHIFT = &HA1
Const VK_LCONTROL = &HA2
Const VK_RCONTROL = &HA3
Const VK_LMENU = &HA4
Const VK_RMENU = &HA5

Example:

' Display the current status of the Enter key.
Dim keystate As Long  ' receives key state

' Read the current status of the Enter key.
keystate = GetKeyState(VK_RETURN)

' Display the Enter key's toggle status.
If (keystate And &H1) = &H1 Then
  Debug.Print "The Enter key is currently toggled."
Else
  Debug.Print "The Enter key is not currently toggled."
End If
' Display whether the Enter key is being pressed or not.
If (keystate And &H80) = &H80 Then
  Debug.Print "The Enter key is currently pressed."
Else
  Debug.Print "The Enter key is currently up."
End If

Related Functions

GetAsyncKeyState, GetKeyboardState

Category

Keyboard

Back to the index.


Last Modified: September 4, 1999
This page is copyright © 1999 Paul Kuliniewicz. Copyright Information

Go to Paul Kuliniewicz's Home Page.
E-mail: rogue953@hotmail.com
This page is at http://skyscraper.fortunecity.com/transmission/45/api/g/getkeystate.html