GetKeyboardState Function

Declare Function GetKeyboardState Lib "user32.dll" (lpKeyState As Byte) As Long

Platforms

Description & Usage

GetKeyboardState retrieves the state of every key on the keyboard and places the information into an array. Each element of the 256-element array identifies information about the virtual-key whose virtual-key code matches the index of the element. If the &H1 bit is set, that key is toggled. If the &H80 bit is set, the key is currently pressed down. 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 an error occured, the function returns 0 (use GetLastError to get the error code). If successful, the function returns a non-zero value.

Visual Basic-Specific Issues

None.

Parameters

lpKeyState
A 256-element byte array which receives the key status information for all virtual-keys. Each key is identified by the element corresponding with the key's virtual key code. Windows NT, 2000: In addition to the virtual keys, the array also receives information which distinguish between the left and right Ctrl, Alt, and Shift keys, which are placed in the array at the following indices:
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.

' Set the toggle status for every key on the keyboard to "not
' toggled."  This change only applies to the current thread.
Dim keystates(0 To 255) As Byte  ' holds states of entire keyboard
Dim c As Integer  ' counter variable
Dim retval As Long  ' return value

' First, get the current state of the keyboard.
retval = GetKeyboardState(keystates(0))

' Now, loop through each element and explicitly set the toggle bit to 0.
For c = 0 To 255
  ' Make sure the &H1 bit is not set.
  keystates(c) = keystates(c) And (Not &H1)
Next c

' Finally, set this to the current keyboard state.
retval = SetKeyboardState(keystates(0))

See Also

GetKeyState, SetKeyboardState

Category

Keyboard

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


Last Modified: September 5, 1999
This page is copyright © 1999 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/getkeyboardstate.html