GetAsyncKeyState Function

Declare Function GetAsyncKeyState Lib "user32.dll" (ByVal vKey As Long) As Integer

Platforms

Description & Usage

GetAsyncKeyState determines whether a certain key is currently pressed and whether that key has been pressed since the last call to the function. This function fails if the thread calling it does not currently have the input focus.

Return Value

If the function fails (if the current thread does not have the input focus), the function returns 0. If the &H1 bit of the return value is set, the key has been pressed at least once since the last time the thread called GetAsyncKeyState. If the &H80 bit of the return value is set, the key is currently pressed down.

Visual Basic-Specific Issues

None.

Parameters

vKey
The virtual-key code of the key to check. 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

' Determine whether the Q key has been pressed or not since
' the last time the program called the function.  Obviously, this
' example assumes the GetAsyncKeyState has already been
' called by the program earlier.
Dim result As Integer  ' result of the function

' Get the state of the Q key as returned by the function.
result = GetAsyncKeyState(VK_Q)  ' get status of the Q key
' Check the &H1 bit of the return value.
If (result And &H1) = &H1 Then
  Debug.Print "The Q key has been pressed since the last call to GetAsyncKeyState."
Else
  Debug.Print "The Q key has not been pressed since the last call to GetAsyncKeyState."
End If

Related Function

GetKeyState

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/getasynckeystate.html