Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Tab key

Posted on 1999-06-30
Medium Priority
Last Modified: 2008-03-10
How to intercept a Tab Key for a listbox and Textbox controls? KeyPress, KeyDown and KeyUp events do not recognize Tab key at all.
Question by:burevestnik
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2

Accepted Solution

jlowrance earned 300 total points
ID: 1521122
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.

Expert Comment

ID: 1521123
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.

Expert Comment

ID: 1521124
wpd - Yes, I'm interested.  Always have an ear open for learning...
Email me at if you wish to continue outside of this forum...

Expert Comment

ID: 1521125
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


Author Comment

ID: 1521126
I believe I found solution myself. I just had to change TabIndex property to do what I need to do. Thanks everyone who responded.

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question