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.

Declare

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

Code

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
            txtPhone.SetFocus
        End If
    End If
End Sub

 

Tip Submitted By: Deborah Kurata

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s