Results 1 to 5 of 5

Thread: How to detect TAB

  1. #1
    Guest

    Question

    Hi everyone,
    Does anyone know how TAB key can be detected, KeyPress event doesn't recognise it. Is there any API that would serve the purpose?

  2. #2
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    KeyPress does recognize it...

    Code:
    Private Sub Form_KeyPress(KeyAscii As Integer)
    If KeyAscii = 9 Then MsgBox "TAB!"
    End Sub
    
    Private Sub Form_Load()
    KeyPreview = True
    End Sub
    ... only at forms with only 1 or less controls.

    I think there was a question about this earlier.
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  3. #3
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Question what r u detecting it in?

    the chr# is 9 for a tab...u shouldn't have any problem detecting it!?

    Code:
    if KeyAscii = 9 then
        Msgbpx "Tab Pressed"
    end if
    I just tested this with:
    Code:
    Private Sub Form_KeyPress(KeyAscii As Integer) ' form
    Private Sub Text1_KeyPress(KeyAscii As Integer) 'text box
    worked fine
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  4. #4
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    hey cool

    I coded a workaround.

    Code:
    'Set all the tabstop of all controls to false
    Private Declare Function GetFocus Lib "user32" () As Long
    Private Sub Form_KeyPress(KeyAscii As Integer)
    Dim h&
    If KeyAscii = vbKeyTab Then 'vbKeyTab = ASCII char 9, but constants look better imho (in my humble opinion ;))
        h = GetFocus 'gets the handle of the control in focus.
        If h = Text1.hWnd Then MsgBox "TAB AT TEXT1" 'reminds you that text1 has focus.
            Text2.SetFocus 'then move on to the next control, just as normal.
    End If
    End Sub
    
    Private Sub Form_Load()
    KeyPreview = True 'receive all keystrokes before the controls do (sort of subclassing, but then easy :))
    End Sub
    Hope it helps ya, it works well for me


    [Edited by Jop on 11-29-2000 at 09:21 AM]
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  5. #5
    Guest

    Cool

    Thank a lot for your input, but my problem is that I have quite few controls on the form and when TAB is pressed the app doesn't go to KeyPress event. Seems like Jop's code could work in this scenario, but I haven't tried it. I worked around this problem assigning Up and Down arrow keys instead of TAB, this is more sensible solution anyway, just have to convince the client.

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