Created: March 1, 1995
When developing an application in Visual Basic®, you may need to interrupt a time-consuming function or react to a certain keypress. It is always a user-friendly option if you allow your user to exit out of these long processes gracefully. The Windows® GetAsyncKeyState application programming interface (API) function can be used to detect when the user has pressed a certain key on the keyboard or clicked a mouse button.
The Windows® GetAsyncKeyState application programming interface (API) function can tell you if a user has clicked a mouse button or pressed a specific key on the keyboard. To declare this function within your program, include the following Declare statement in the Global Module or General Declarations section of a Visual Basic® form:
Declare Function GetAsyncKeyState Lib "User" (ByVal Key As Integer) As Integer
Note that this Declare statement must be typed as one single line of text.
The GetAsyncKeyState function determines if the specific key was pressed since the last call to this function. It can also tell you if one of the mouse buttons was pressed. This function takes only one argument—an integer value that represents the key code of the virtual key or mouse button you want to test.
GetAsyncKeyState returns a non-zero value if the specified key or button is currently pressed. It will also return a non-zero value if the key or button was pressed since the last call to this function. The CONSTANT.TXT file contains a list of the virtual key constants.
The following Visual Basic program shows how you can perform a function within your program until a specific keystroke is detected on the keyboard. In this example program, the DoEvents function is called to put the program in a constant loop. When you press the ESC (escape) key, the program is terminated.
Declare Function GetAsyncKeyState Lib "User" (ByVal Key As Integer) As Integer
Const KEY_ESCAPE = &H1B
Sub Command1_Click()
Text1.Text = "Press ESCAPE to quit"
Do While DoEvents()
If GetAsyncKeyState(KEY_ESCAPE) Then
Text1.Text = "ESCAPE pressed"
Exit Sub
End If
Loop
End Sub