Home All Groups Group Topic Archive Search About
Author
23 Sep 2005 6:18 PM
JoeBananas
Ok, I've searched and searched and searched and can only find one
answer to this but I can't use that soluction.

I know the Tab key can only be intercepted in the KeyPress event and
not the KeyUp or KeyDown events. I know that all the controls on the
form have to have the TapStop property set to False in order to
capture the Tab key. But how can I intercept a Shift+Tab key
combination. KeyPress doesn't give you the Shift state only KeyUp and
KeyDown do. How can you really intercept the Tab key in the KeyUp and
KeyDown events.

The only way I've found is to hook the Message loop but that makes
debugging in the IDE near impossible.

Does anybody have a good idea for this?

Thanks!

Author
23 Sep 2005 6:47 PM
Jeff Johnson [MVP: VB]
Show quote Hide quote
"JoeBananas" <Some***@aplace.net> wrote in message
news:l8h8j1lt2g2j50e1puhq2k09kjbfqhblkc@4ax.com...

> Ok, I've searched and searched and searched and can only find one
> answer to this but I can't use that soluction.
>
> I know the Tab key can only be intercepted in the KeyPress event and
> not the KeyUp or KeyDown events. I know that all the controls on the
> form have to have the TapStop property set to False in order to
> capture the Tab key. But how can I intercept a Shift+Tab key
> combination. KeyPress doesn't give you the Shift state only KeyUp and
> KeyDown do. How can you really intercept the Tab key in the KeyUp and
> KeyDown events.
>
> The only way I've found is to hook the Message loop but that makes
> debugging in the IDE near impossible.
>
> Does anybody have a good idea for this?

Search also for "GetKeyState".
Author
23 Sep 2005 8:00 PM
JoeBananas
thanks for the quick reply.


That's fine but, KeyUp and Keydown don't fire when the tab key is
pressed so how do I test GetKeyState when no event has fired.

Thanks


On Fri, 23 Sep 2005 14:47:39 -0400, "Jeff Johnson [MVP: VB]"
<i.get@enough.spam> wrote:

Show quoteHide quote
>
>"JoeBananas" <Some***@aplace.net> wrote in message
>news:l8h8j1lt2g2j50e1puhq2k09kjbfqhblkc@4ax.com...
>
>> Ok, I've searched and searched and searched and can only find one
>> answer to this but I can't use that soluction.
>>
>> I know the Tab key can only be intercepted in the KeyPress event and
>> not the KeyUp or KeyDown events. I know that all the controls on the
>> form have to have the TapStop property set to False in order to
>> capture the Tab key. But how can I intercept a Shift+Tab key
>> combination. KeyPress doesn't give you the Shift state only KeyUp and
>> KeyDown do. How can you really intercept the Tab key in the KeyUp and
>> KeyDown events.
>>
>> The only way I've found is to hook the Message loop but that makes
>> debugging in the IDE near impossible.
>>
>> Does anybody have a good idea for this?
>
>Search also for "GetKeyState".
>
Author
23 Sep 2005 8:47 PM
Jeff Johnson [MVP: VB]
"JoeBananas" <Some***@aplace.net> wrote in message
news:hkn8j1l31lrb9bnaol6bo2on19kmcmuk15@4ax.com...

> That's fine but, KeyUp and Keydown don't fire when the tab key is
> pressed so how do I test GetKeyState when no event has fired.

Do it in the event you DO get: KeyPress.
Author
23 Sep 2005 9:32 PM
Ken Halter
If you drop a timer on a form and run this, you'll see a way to get the same
Shift key status as KeyDown/Up give you.....
'================
Option Explicit

Private Declare Function GetKeyboardState _
                Lib "user32.dll" (ByRef pbKeyState As Byte) As Long

Private Function GetShiftStatus() As ShiftConstants
   Dim btArray(255) As Byte
   Dim eRet As ShiftConstants

   Call GetKeyboardState(btArray(0))

   If btArray(vbKeyMenu) > 127 Then
      eRet = vbAltMask
      Debug.Print "Alt is down",
   End If

   If btArray(vbKeyControl) > 127 Then
      eRet = eRet Or vbCtrlMask
      Debug.Print "Ctrl is down",
   End If

   If btArray(vbKeyShift) > 127 Then
      eRet = eRet Or vbShiftMask
      Debug.Print "Shift is down";
   End If

   If eRet <> 0 Then
      Debug.Print Timer
   End If

   GetShiftStatus = eRet

End Function

Private Sub Timer1_Timer()
   Call GetShiftStatus
End Sub
'================

You can check that in the KeyPress event... problem is, Ctrl-Tab doesn't
make it to KeyPress. Instead, it passes a "9" to KeyDown, leaving it up to
you to check the shift status... Here's the code I'm testing with.... ran
out of time to do anything "real" so.....
'==========
Option Explicit

Private meShiftStatus As ShiftConstants

Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
   Debug.Print "'Form1:Text1_KeyDown", KeyCode, Timer
   meShiftStatus = Shift
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
   Debug.Print "'Form1:Text1_KeyPress", KeyAscii, Timer
   If KeyAscii = vbKeyTab Then
      Debug.Print "Tab Key + " & meShiftStatus
   End If
End Sub
'==========

--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
Please keep all discussions in the groups..
Show quoteHide quote
"JoeBananas" <Some***@aplace.net> wrote in message
news:hkn8j1l31lrb9bnaol6bo2on19kmcmuk15@4ax.com...
> thanks for the quick reply.
>
>
> That's fine but, KeyUp and Keydown don't fire when the tab key is
> pressed so how do I test GetKeyState when no event has fired.
>
> Thanks
>
>
> On Fri, 23 Sep 2005 14:47:39 -0400, "Jeff Johnson [MVP: VB]"
> <i.get@enough.spam> wrote:
>
>>
>>"JoeBananas" <Some***@aplace.net> wrote in message
>>news:l8h8j1lt2g2j50e1puhq2k09kjbfqhblkc@4ax.com...
>>
>>> Ok, I've searched and searched and searched and can only find one
>>> answer to this but I can't use that soluction.
>>>
>>> I know the Tab key can only be intercepted in the KeyPress event and
>>> not the KeyUp or KeyDown events. I know that all the controls on the
>>> form have to have the TapStop property set to False in order to
>>> capture the Tab key. But how can I intercept a Shift+Tab key
>>> combination. KeyPress doesn't give you the Shift state only KeyUp and
>>> KeyDown do. How can you really intercept the Tab key in the KeyUp and
>>> KeyDown events.
>>>
>>> The only way I've found is to hook the Message loop but that makes
>>> debugging in the IDE near impossible.
>>>
>>> Does anybody have a good idea for this?
>>
>>Search also for "GetKeyState".
>>
>
Author
23 Sep 2005 9:34 PM
Ken Halter
"JoeBananas" <Some***@aplace.net> wrote in message
news:hkn8j1l31lrb9bnaol6bo2on19kmcmuk15@4ax.com...
> thanks for the quick reply.

BTW... hooks are only dangerous if you forget they're active <g> You can
also build a "mini" dll that contains the hook code, instantiate and use
that at runtime. That shouldn't crash when you pause the app. Once debugged,
grab the code from that "mini" dll and incorporate it into the app.

--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
Please keep all discussions in the groups..
Author
24 Sep 2005 12:54 AM
Ben Amada
JoeBananas wrote:

> thanks for the quick reply.
>
>
> That's fine but, KeyUp and Keydown don't fire when the tab key is
> pressed so how do I test GetKeyState when no event has fired.
>
> Thanks

I've used the Validation event before which will fire when a control is just
about to lose focus.  In the Validation event, you can use GetKeyState API
and stop the control from losing focus if you need to.

Ben