Capturing The Tab Key

This is not an easy task because the TAB does NOT generate the Key_Press event. So, you need to use the GetKeyState windows API function for this.


Private Declare Function GetKeyState Lib "User32" (ByVal nVirtKey As Long) As Integer
' Virtual key values
Const VK_TAB = &H9
Const VK_SHIFT = &H10


Sub txtAreaCode_LostFocus()
Dim iRetVal As Integer
    ' Check for a tab out of this control
    ' Skip the state field
    iRetVal = GetKeyState(VK_SHIFT)
    ' If the shift was NOT on, check the tab
    If iRetVal <> -128 And iRetVal <> -127 Then
        iRetVal = GetKeyState(VK_TAB)
        If iRetVal = -128 Or iRetVal = -127 Then ' tab key pressed
        End If
    End If
End Sub


Tip Submitted By: Deborah Kurata