Tab key

How to intercept a Tab Key for a listbox and Textbox controls? KeyPress, KeyDown and KeyUp events do not recognize Tab key at all.
Who is Participating?
jlowranceConnect With a Mentor Commented:
If no control on the form can accept focus from a tab keypress, then a KeyDown and KeyPress event will be generated.

Try this - create code in the GotFocus event of the textbox and listbox to set the TabStop to False for all controls on the form, including themselves.  This will prevent the Tab from setting focus to another control.

Now you can check for a KeyAscii of 9 in the KeyPress event or a KeyCode of 9 in the KeyDown event.

After the Tab is detected, you can perform whatever you wish to do and also turn on the TabStop = True for all controls and then setfocus to whatever control you like.

Be sure that in the LostFocus event of the listbox and textbox that you turn on TabStop = True for all controls so that navigation is enabled again.
Or you can subclass the window and trap all events _before_ VB gets a chance to process and/or discard them. I'll tell you more if you're interested.
wpd - Yes, I'm interested.  Always have an ear open for learning...
Email me at if you wish to continue outside of this forum...
See below. Should add plenty of error checking and such, but this code will let you peek at all window messages before VB gets a chance to process them.
Const GWL_WNDPROC = -4

Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpFunc&, ByVal hWnd&, ByVal Msg&, ByVal wParam&, ByVal lParam&) as Long
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd&, ByVal index&, ByVal dwNewLong&) as Long

Dim lSavWndProc as Long

Private Sub Form_Load()

' subclass the window
lSavWndProc = SetWindowLong(Me.hWnd, GWL_WNDPROC, AddressOf myWindowProc)

End Sub

Private Sub Form_Unload()

 ' remove the hook
 Call SetWindowLong (Me.hWnd, GWL_WNDPROC, lSavWndProc)

End Sub

' our custom window proc
Function myWindowProc(ByVal hWnd&, ByVal msg&, ByVal wParam&, ByVal lParam&) as Long

if msg = ... ' Test for the message that interests you
'... do something with it

' pass the message for normal processing
myWindowProc = CallWindowProc (lSavWndProc, hWnd, msg, wParam, lParam)

End Function

burevestnikAuthor Commented:
I believe I found solution myself. I just had to change TabIndex property to do what I need to do. Thanks everyone who responded.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.