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?
Printable View
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?
KeyPress does recognize it...
... only at forms with only 1 or less controls.Code:Private Sub Form_KeyPress(KeyAscii As Integer)
If KeyAscii = 9 Then MsgBox "TAB!"
End Sub
Private Sub Form_Load()
KeyPreview = True
End Sub
I think there was a question about this earlier.
the chr# is 9 for a tab...u shouldn't have any problem detecting it!?
I just tested this with:Code:if KeyAscii = 9 then
Msgbpx "Tab Pressed"
end if
worked fineCode:
Private Sub Form_KeyPress(KeyAscii As Integer) ' form
Private Sub Text1_KeyPress(KeyAscii As Integer) 'text box
hey cool :cool:
I coded a workaround.
Hope it helps ya, it works well for me ;)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
[Edited by Jop on 11-29-2000 at 09:21 AM]
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:).