CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2001
    Posts
    5

    Know the form tab press at form level

    I have set the Form KeyPreview-True, to trap the
    key events at form level, but how do i trap
    the tab key. It is not responding at all on
    tab press.

    Please suggest.


  2. #2
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: Know the form tab press at form level

    I can detect the tab key in the From_KeyDown Event.

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



    produces a "9" in the msgbox

    John G

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

    Re: Know the form tab press at form level

    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