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 &H8000 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

' This code is licensed according to the terms and conditions listed here.

' Declarations and such needed for the example:
' (Copy them to the (declarations) section of a module.)
Public Declare Function GetKeyState Lib "user32.dll" (ByVal nVirtKey As Long) As Integer

' Determine whether the Q key is currently being pressed.
' The code runs when button Command1 is pressed.

Private Sub Command1_Click()
	Dim keystate As Integer  ' state of the Q key
	
	' Get the state of the Q key as returned by the function.
	' (vbKeyQ is a VB-defined constant for Q's virtual-key code)
	keystate = GetKeyState(vbKeyQ)
	' Check the &H8000 bit of the return value.
	If keystate And &H8000 Then
		Debug.Print "The Q key is currently down."
	Else
		Debug.Print "The Q key is currently up."
	End If
End Sub

See Also

GetAsyncKeyState, GetKeyboardState

Category

Keyboard

Go back to the alphabetical Function listing.
Go back to the Reference section index.


Last Modified: July 30, 2000
This page is copyright © 2000 Paul Kuliniewicz. Copyright Information Revised October 29, 2000
Go back to the Windows API Guide home page.
E-mail: vbapi@vbapi.com Send Encrypted E-Mail
This page is at http://www.vbapi.com/ref/g/getkeystate.html