Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Trap TAB Key before LostFocus ? 3

Status
Not open for further replies.

nomi2000

ISP
Feb 15, 2001
676
CA
Dear Fellow Programmers
i want to catch if Tab key is pressed before lost focus fired?
i am not being able to catch like
If KeyCode=vbTab then
in any of the Textbox event
how do i do this?
Regards
Nouman

 
You could use the KeyPress event of the textbox.

Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 9 Then
'tab has been pressed
End If
End Sub
 
pokermat,

That will only work if all the controls on the form have there tab stop property's set to false, otherwise the tab key moves the focus to the next control in the tabstop sequence. Anything is possible, the problem is I only have one lifetime.
[cheers]
 
Sorry I called off to do my job [curse]. What you will need to do is Hook the keyboard. What are you looking to do if the tab key is pressed so I can get you some code? Anything is possible, the problem is I only have one lifetime.
[cheers]
 
Yes Foda i want to need the code of hooking the keyboard but i m really disappointed that VB can't does such a simple task and you have to go inot hooking with complex APIs isn't it not fare :)
Regards
Nouman
 
The problem is that you are using a key that is generally used by the system only, therefore Uncle Bill Gates thought it would be best if the average Joe could not get at it. Anyway here you go.

In a form place this code

Public Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal nCode As Long, ByVal wParam As Long, _
ByVal lParam As Long) As Long

Public Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long

Public Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, _
ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long

Public Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long

Public Const WH_KEYBOARD = 2
Public Const KBH_MASK = &H20000000
Public Const WM_LBUTTONDOWN = &H201
Public Const WM_LBUTTONUP = &H202
Public Const WM_KEYDOWN = &H100
Public Const WM_KEYUP = &H101
Public Const VK_TAB = &H9

Global hHook As Long

Public Function KeyboardProc(ByVal nCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

If nCode >= 0 Then
'Process keys you want to filter
If wParam = VK_TAB Then
If (lParam And &HC0000000) = 0 Then
Form1.Print "Tab"
KeyboardProc = 1
Exit Function
End If
End If
End If
KeyboardProc = CallNextHookEx(hHook, nCode, wParam, lParam)
End Function

Hopefully this will get you started. Gook Luck. Anything is possible, the problem is I only have one lifetime.
[cheers]
 
Some how that code got messed up it the post, use this


In the main form

Private Sub Form_Load()
hHook = SetWindowsHookEx(WH_KEYBOARD, AddressOf KeyboardProc, 0&, App.ThreadID)
End Sub

Private Sub Form_Unload(Cancel As Integer)
Call UnhookWindowsHookEx(hHook)
End Sub


In the standard module


Public Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal nCode As Long, ByVal wParam As Long, _
ByVal lParam As Long) As Long

Public Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long

Public Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, _
ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long

Public Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long

Public Const WH_KEYBOARD = 2
Public Const KBH_MASK = &H20000000
Public Const WM_LBUTTONDOWN = &H201
Public Const WM_LBUTTONUP = &H202
Public Const WM_KEYDOWN = &H100
Public Const WM_KEYUP = &H101
Public Const VK_TAB = &H9

Global hHook As Long

Public Function KeyboardProc(ByVal nCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

If nCode >= 0 Then
'Process keys you want to filter
If wParam = VK_TAB Then
If (lParam And &HC0000000) = 0 Then
Form1.Print "Tab"
KeyboardProc = 1
Exit Function
End If
End If
End If
KeyboardProc = CallNextHookEx(hHook, nCode, wParam, lParam)
End Function
Anything is possible, the problem is I only have one lifetime.
[cheers]
 
Dear Foada
Thanks for supplying the code well i got the "TAB" print at Form but now the Tab is not working i have to tab in the usual manners the focus is not moving to next control besides where in the code i have to check that "TAB" key is pressed in the TextBox?
Regards
Nouman
 
That is why I asked what you wanted to d with the tab key. Try this

Private bToggle As Boolean

Public Function KeyboardProc(ByVal nCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

If nCode >= 0 Then
If wParam = VK_TAB Then
If (lParam And &HC0000000) = 0 Then
HandleTabKey
Call PostMessage(Form1.Text1.hwnd, _
WM_KEYDOWN, _
VK_TAB, _
0)

KeyboardProc = 1
Exit Function
End If
End If
End If
KeyboardProc = CallNextHookEx(hHook, nCode, wParam, lParam)
End Function

Public Sub HandleTabKey()

bToggle = Not bToggle
If bToggle Then
MsgBox "Tab Key Pressed"
End If
End Sub


Hope this helps Anything is possible, the problem is I only have one lifetime.
[cheers]
 
dear foada
yes i have done this but still some problem
a) when i am pressing SHIFT+TAB key the TAB code is fires and Msgbox shown why this happening
b)In the code you provide
Call PostMessage(Form1.Text1.hwnd, _
WM_KEYDOWN, _
VK_TAB, _
0)

do i have to Pass the first paramete for each control which i have to TAB check pressed or the constant "Form1.Text1.hwnd"
when i changed the function of "KeyboardProc" with an additional parameter of Control to handle TAB key on that control it is not working and closing the program abruptly..

Regards
nouman
 
I am not real clear as to what you are trying to do with the shift key. The basic function of the code goes like this

If wParam = VK_TAB Then 'Checks if it is the tab key


This part actually sends the tab key to pass the focus to the next control in the tab order. If you do not use this the tab key is not pressed.

Call PostMessage(Form1.Text1.hwnd, _
WM_KEYDOWN, _
VK_TAB, _
0)

What are you doing with the tab key and the shift key? I can adjust the code if you can tell me what you are trying to do. Anything is possible, the problem is I only have one lifetime.
[cheers]
 
Dear Foada
okay here i go the problem which i am facing is that i won't able to catch the TAB-Key in the textbox when i pressed Access-Key Sat CTL+S on a Command Button i over come this prob by inserting DoEvents commmand in the Click Event of the button and that problem solved but the problem which i faced and i asked in this Post is that say i have so many textboxes and command button in it now when i pressed the TAB-Key i won't able to catch if the key pressed is TAB so i could turn some boolean flag ON and OFF and based on it i could by-passs some code.although the code you provides does well in TABing and also catching if the Key Pressed is TAB..but it also fires the event
"HandlKey" Function When i pressed Shift+TAB to goes back ..Shift+TAB works opposite to TAB but still "HandleKey"
function fires
You said
If wParam = VK_TAB Then 'Checks if it is the tab key
but this condition also true when i pressed SHIFT+TAB to goes back to the control..now this i don't want because its not the TAB which is pressed but SHIFT+TAB which is pressed so it should not fires "HandleKey"e event
Thanks for your help your help is very good for me in seeking the right direction...
Regards
Nouman




Regards
Nouman
 
Replace these two procedures

Public Function KeyboardProc(ByVal nCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

If nCode >= 0 Then
'Process keys you want to filter
If wParam = VK_TAB Then
If (lParam And &HC0000000) = 0 Then
If HandleTabKey Then
Call PostMessage(Form1.Text1.hwnd, _
WM_KEYDOWN, _
VK_TAB, _
0)
KeyboardProc = 1
Exit Function
End If
End If
End If
End If
KeyboardProc = CallNextHookEx(hHook, nCode, wParam, lParam)
End Function

Public Function HandleTabKey() As Boolean
Dim ans As VbMsgBoxResult

bToggle = Not bToggle
If bToggle Then
ans = MsgBox("Do You Wish To Fire The Tab Key ?", vbYesNo, "Tab Key Trap")
If ans = vbYes Then
HandleTabKey = True
End If
End If
End Function


If I am understanding you correctly the shift-tab should not affect things. The code I just posted hooks the tab key and then ask if you would like to press the tab key. If you answer no the tab key press is disgarded. If you answer yes the tab key is then pressed and things will work as normal. Just add what ever code you need to the HandleTabKey function to decide if the app sees the tab key or not. If you would like this to work on several form then you will need to add the formload and formunload code to each of those forms and then pass the forms hwnd to the KeyboardProc. Hope this helps. Anything is possible, the problem is I only have one lifetime.
[cheers]
 
Dear Foada
Thanks so much for ur help..I will check it at off i have some q,u said just pass the Form hwnd my q is how?
in the post message the hwnd is the handle of texbox? how i make it for each textbox in the Form? do i have to hook for each textbox seperately? and what about if i have another form with textboxes how i handle this?
Call PostMessage(Form1.Text1.hwnd, _
WM_KEYDOWN, _
VK_TAB, _
0)
Regards
Noman

 
In the module add this declaration

Public hWinHandle As Long

and in the Controls Got Focus Event add

Private Sub Text1_GotFocus()
hWinHandle = Text1.hwnd
End Sub

Then change this code

Call PostMessage(hWinHandle, _
WM_KEYDOWN, _
VK_TAB, _
0)

That should take care of it.
Anything is possible, the problem is I only have one lifetime.
[cheers]
 
FYI there's another way to do this that is much simpler, you insert 2 unvisible control in your form and set the tabkey before and after the control you want to trap. IE, if the tabkey of your control is 5, you set the first control to 4 and the second to 6. Then you catch the tab key when control 4 or 6 get the focus. I do this with a grid control and it works perfectly, I couldn't navigate the cells before, it was jumping me to the next control but now it work's great. You just put a setfocus to the control 5 in the getfocus code of the conrol 4&6 if you want to stay in the same control.

Hope this helps
 
Dear Foada
thanx alot for ur help i got it finally ,thanks again for ur help
Regards
Nouman
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top