CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: tab

  1. #1
    Join Date
    Jan 2001
    Posts
    2

    tab

    hi ,

    I would like if there is a way to intercept when a user presse the tab key on a control button.

    thank you
    Jean


  2. #2
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: tab

    Catching keys (tab, arrows) without subclassing is not easy.

    Form_KeyDown (or Form_KeyUp) will not catch all keys always.

    If form has no controls, it will work.
    If you add one textbox, it will work.

    If you add second textbox, it will work for arrow keys, but Tab key will be 'eaten'.

    If you add few buttons, VB will allow you to use arrow keys for navigation between buttons. But then
    ... Form_KeyDown will not be able to catch these arrow keys.

    Here is the solution - it uses one API. You will be able to catch Tab key and arrow keys. If Form_KeyDown
    is not able to receive a key, keyCheckTab or keyCheck will do it.

    ' Form1 (KeyPreview = true); add two textxboxes and two buttons
    option Explicit
    private Declare Function GetKeyState Lib "user32" (byval nVirtKey as Long) as Integer

    private Sub Form_KeyDown(KeyCode as Integer, Shift as Integer)
    print KeyCode
    End Sub

    private Sub Command1_LostFocus()
    ' add this code to LostFocus event of your buttons
    keyCheck
    End Sub

    private Sub Command2_LostFocus()
    keyCheck
    End Sub

    private Sub Text1_LostFocus()
    ' add this code to LostFocus event of your textboxes
    keyCheckTab
    End Sub

    private Sub Text2_LostFocus()
    ' add this code to LostFocus event of your textboxes
    keyCheckTab
    End Sub

    private Sub keyCheck()
    If GetKeyState(vbKeyTab) < 0 then
    print "Tab pressed"
    ElseIf GetKeyState(vbKeyLeft) < 0 then
    print "Left pressed"
    ElseIf GetKeyState(vbKeyRight) < 0 then
    print "Right pressed"
    ElseIf GetKeyState(vbKeyUp) < 0 then
    print "Up pressed"
    ElseIf GetKeyState(vbKeyDown) < 0 then
    print "Down pressed"
    End If
    End Sub

    private Sub keyCheckTab()
    If GetKeyState(vbKeyTab) < 0 then
    print "Tab pressed"
    End If
    End Sub


    Iouri Boutchkine
    [email protected]
    Iouri Boutchkine
    [email protected]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured