Tip 152: Determining the Status of Virtual Keys on the Keyboard

September 5, 1995

Abstract

Within a Microsoft® Visual Basic® application, you can determine the status of any virtual key on the keyboard. This article explains how to retrieve and set the status of virtual keys.

Toggling the Status of a Specific Key on the Keyboard

In a Microsoft® Visual Basic® application, you can use the GetKeyboardState function to retrieve the current status of any key on the keyboard. To use this function in your program, you must add the following Declare statement to the General Declarations section of your form:

Private Declare Sub GetKeyboardStateByString Lib "user32" Alias 
   "GetKeyboardState" (ByVal pbKeyState As String)

The SetKeyboardState function is used to change the status of one or more keys on the keyboard. Its Declare statement is as follows:

Private Declare Sub SetKeyboardStateByString Lib "user32" Alias 
   "SetKeyboardState" (ByVal lppbKeyState As String)

Both the GetKeyboardState and SetKeyboardState functions require one argument—a buffer large enough to hold the status of all 256 virtual keys on the keyboard. Each byte in this array corresponds to one virtual key. If a toggle key, such as num lock, is off, the low-order bit of its status byte is 0. On the other hand, if the toggle key is on, the low-order bit is 1. For other virtual keys, the key is down if the high-order bit is 1, and the key is up if the high-order bit is 0.

You can use the example program below to toggle the state of the num lock key. You do this by first calling the GetKeyboardState function to retrieve the status of all 256 virtual keys. A 256-byte string holds this information. Next, you isolate (using the Mid$ function) the byte that corresponds to the num lock key.

When you want to turn the num lock key on, you set its status to 1. Alternatively, if you want to toggle the num lock key off, you set its status to 0.

The final step is to tell the operating system that you have changed the status of a virtual key on the keyboard. You do this by calling the SetKeyboardState function, which copies the new keyboard status array to the operating system. When the program carries out this function, the status of the num lock key is immediately changed.

Example Program

This program shows how to turn the num lock key on and off.

  1. Create a new project in Visual Basic. Form1 is created by default.

  2. Add the following Constant and Declare statements to the General Declarations section of Form1 (note that each Declare statement must be typed as a single line of code):
    Private Declare Sub GetKeyboardStateByString Lib "user32" Alias 
       "GetKeyboardState" (ByVal pbKeyState As String)
    Private Declare Sub SetKeyboardStateByString Lib "user32" Alias 
       "SetKeyboardState" (ByVal lppbKeyState As String)
    Const VK_NUMLOCK = &H90
    
  3. Add a Command Button control to Form1. Command1 is created by default. Set its Caption property to "On".

  4. Add the following code to the Click event for Command1:
    Private Sub Command1_Click()
        Dim NumLockKey As String * 256
        NumLockKey = Space$(256)
        GetKeyboardStateByString (NumLockKey)
        Mid$(NumLockKey, VK_NUMLOCK + 1, 1) = Chr$(1)
        Call SetKeyboardStateByString(NumLockKey)
    End Sub
    
  5. Add a second Command Button control to Form1. Command2 is created by default. Set its Caption property to "Off".

  6. Add the following code to the Click event for Command2:
    Private Sub Command2_Click()
        Dim NumLockKey As String * 256
        NumLockKey = Space$(256)
        GetKeyboardStateByString (NumLockKey)
        Mid$(NumLockKey, VK_NUMLOCK + 1, 1) = Chr$(0)
        Call SetKeyboardStateByString(NumLockKey)
    End Sub
    

Run the example program by pressing f5. Click the On command button to turn on the num lock key. Click the Off command button to turn off the num lock key.

Additional References

"GetKeyboardState." (MSDN Library, SDK Documentation, Platform SDK)

"SetKeyboardState." (MSDN Library, SDK Documentation, Platform SDK).

"Virtual-Key Codes." (MSDN Library, SDK Documentation, Platform SDK)