How To Use GetKeyState() to Check If the TAB Key Was Pressed (277916)



The information in this article applies to:

  • Microsoft Visual Basic Professional Edition for Windows 6.0
  • Microsoft Visual Basic Enterprise Edition for Windows 6.0

This article was previously published under Q277916

SUMMARY

When you press the TAB key while the focus is on a control, the focus goes to the next control in the TAB order, and the KeyPress, KeyUp, and KeyDown events of the original control do not fire. As a result, you cannot use these events to trap the TAB key. However, you can use the GetKeyState function to check if the TAB key was pressed.

MORE INFORMATION

To check if the TAB key was pressed, follow these steps:
  1. Start a Standard EXE project in Visual Basic. Form1 is created by default.
  2. Add a Textbox and a CommandButton to Form1. Set the CausesValidation property of the Textbox to True.
  3. In the code module of Form1, add the following code:
    Option Explicit
    
    Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
    
    Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
       Debug.Print "in keyDown"
    End Sub
    
    Private Sub Text1_KeyPress(KeyAscii As Integer)
       Debug.Print "in keyPress"
    End Sub
    
    Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
       Debug.Print "in keyUp"
    End Sub
    
    Private Sub Text1_Validate(Cancel As Boolean)
       Dim retval As Integer
    
       retval = GetKeyState(vbKeyTab)
       If retval < 0 Then Debug.Print "Tab key was pressed in Text1"
    End Sub
    					
  4. Press the F5 key to run the project.
  5. The focus is in the Textbox. Press the TAB key, and the focus shifts to the CommandButton.
  6. Check the Immediate window. Although the KeyPress, KeyUp, and KeyDown events do not fire, the Validate event indicates that the Tab key was pressed.

Modification Type:MinorLast Reviewed:7/13/2004
Keywords:kbAPI kbCodeSnippet kbhowto KB277916