By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,650 Members | 1,785 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,650 IT Pros & Developers. It's quick & easy.

Anyone know how to capture a Shift+Tab in the KeyPress event?

P: n/a
I am attempting to detect a Shift+Tab in the KeyPress event for back
navigation on a control that doesn't support this method. Does anyone
have any ideas how to compare e.KeyChar to a ShiftTab?

Tim
Nov 20 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Hi Tim,

Some information about keyboard handling:

KeyPress is for the typeable character keys - hence it's KeyChar As Char.
For keys in general you'd use KeyDown.

KeyDown has a KeyEventArgs which gives you Alt, Control and Shift -
boolean properties - to test the shift keys individually, and KeyCode and
KeyData to test the key itself.

KeyCode will be the key code, Keydata is that same key combined with
shift-key information. Then, just to give you even more choice, there's
Modifier which gives you just the shift-key information combined into a single
property.

Bear in mind that KeyDown will occur once for Shift and then again for the
key that's being shifted.

Sub KeyPress (.., e As KeyEventData)
If e.Shift And e.KeyCode = Keys.T Then
'We've got a capital T.

But!!

When it comes to certain keys, such as Tab and Alt-F (when there is a menu
item with a hotkey, eg "&File"), they are processed even before KeyDown gets
to see them. So to get to <these> keys you have to hook into the key
processing sequence a bit earlier.

There's a whole stack of methods that process keys. The one that you want,
ProcessCmdKey, is called to deal with the hotkeys. You can override this and
test whether Tab has been pressed. If so, you can deal with it yourself.
Having done that, you pass True back to say that you don't want the default
behaviour. If you don't deal with a key, you should allow your base class to
do so.

I'm curious. Can I ask what you want to trap Shift-Tab for?

Regards,
Fergus

<code>
Protected Overrides Function ProcessCmdKey (ByRef msg As Message, _
ByVal keyData As Keys)
As Boolean
If KeyData = (Keys.Shift + Keys.Tab) Then
'Now we're on to something!
: : :
Return True 'Prevent the normal Shift-Tab behaviour
End If
Return MyBase.ProcessCmdKey 'Other keys work normally.
End Function
</code>
Nov 20 '05 #2

P: n/a

Hi Tim,

You can override the ProcessKeyPreview method, then handle the WM_KEYUP
message.
Sample code like this:

Public Const WM_KEYUP = &H101
Public Const VK_TAB = &H9

Protected Overrides Function ProcessKeyPreview(ByRef m As Message) As
Boolean
If m.Msg = WM_KEYUP Then
If m.WParam.ToInt32() = VK_TAB Then
If Control.ModifierKeys = Keys.Shift Then
MessageBox.Show("Shift+Tab")
End If
End If
End If
MyBase.ProcessKeyPreview(m)
End Function

It works well on my machine, if you have anything unclear, please feel free
to let me know.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: ti*********@fishgame.state.ak.us (Tim Frawley)
| Newsgroups: microsoft.public.dotnet.languages.vb
| Subject: Anyone know how to capture a Shift+Tab in the KeyPress event?
| Date: 21 Oct 2003 17:42:27 -0700
| Organization: http://groups.google.com
| Lines: 5
| Message-ID: <bf*************************@posting.google.com>
| NNTP-Posting-Host: 146.63.252.53
| Content-Type: text/plain; charset=ISO-8859-1
| Content-Transfer-Encoding: 8bit
| X-Trace: posting.google.com 1066783347 5456 127.0.0.1 (22 Oct 2003
00:42:27 GMT)
| X-Complaints-To: gr**********@google.com
| NNTP-Posting-Date: Wed, 22 Oct 2003 00:42:27 +0000 (UTC)
| Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed 00.sul.t-online.de!t-onlin
e.de!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!postnew s1.google.com!no
t-for-mail
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vb:148908
| X-Tomcat-NG: microsoft.public.dotnet.languages.vb
|
| I am attempting to detect a Shift+Tab in the KeyPress event for back
| navigation on a control that doesn't support this method. Does anyone
| have any ideas how to compare e.KeyChar to a ShiftTab?
|
| Tim
|

Nov 20 '05 #3

P: n/a
> I'm curious. Can I ask what you want to trap Shift-Tab for?
Fergus,

Thank you for the information. It really helps to understand what
these properties are for rather than just dumping the code into my
application and hoping for the best.

To answer your question I am attempting to create foward and backward
navigation in a datagrid that does not support these functions. When
the press TAB it will navigate through the grid but when the user
reached the last column in the row I wanted to put the focus on the
first column in the next row and when the reach the last column in the
last row and press TAB I wanted to add a row to continue data entry.

The Shift+Tab was to allow the user to backup to the prior column or
if they are in the first column then put the focus in the last column
of the previous row.

A little grid customization to make data entry a little easier for the
user. However, they will have to 'click' off the control to get out.

Tim Frawley
Nov 20 '05 #4

P: n/a
Hi Tim,

Glad the info was helpful. :-)

The reason I asked is that the traping will work for the whole Form so you
will have add a switch to it so that it only traps when the Grid has focus.But
it sounds like you're already on to that one. ;-)

I'm all for making it easier for the User. Given that they'll already be
keyboarding with Tab and Shift-Tab, you could perhaps add Ctrl-Tab and
Ctrl-Shift-Tab (or some such) to get off the Grid and back into the main flow.

Regards,
Fergus
Nov 20 '05 #5

P: n/a

Hi Tim,

Thanks for your feedback.
It seems that the default datagrid keyboard navigation already behaves like
what you want-----tab for next cell, shift+tab for previous cell. So I
think there is no need for doing this customize navigation in datagrid.
Also, as I tried, it seems that the datagrid also use ctrl+tab to jump out
of datagrid to the next control on the form.
Btw: if you want to implement keyboard navigation in datagrid or other
certain control, I think it is more suitable to handle the message in the
control's method but not the form's procedure. For example, you can
override the datagrid control's ProcessKeyPreview method to create a new
customized datagrid. Then, you can reuse this customized datagrid in any
application you want.

Hope this helps, if you still have anything unclear, please feel free to
tell me, I am glad to work with you.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: ti*********@fishgame.state.ak.us (Tim Frawley)
| Newsgroups: microsoft.public.dotnet.languages.vb
| Subject: Re: Anyone know how to capture a Shift+Tab in the KeyPress event?
| Date: 22 Oct 2003 09:56:27 -0700
| Organization: http://groups.google.com
| Lines: 24
| Message-ID: <bf*************************@posting.google.com>
| References: <bf*************************@posting.google.com>
<ub**************@tk2msftngp13.phx.gbl>
| NNTP-Posting-Host: 146.63.252.53
| Content-Type: text/plain; charset=ISO-8859-1
| Content-Transfer-Encoding: 8bit
| X-Trace: posting.google.com 1066841787 17792 127.0.0.1 (22 Oct 2003
16:56:27 GMT)
| X-Complaints-To: gr**********@google.com
| NNTP-Posting-Date: Wed, 22 Oct 2003 16:56:27 +0000 (UTC)
| Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed 00.sul.t-online.de!t-onlin
e.de!newsfeed.icl.net!newsfeed.fjserv.net!news.max well.syr.edu!postnews1.goo
gle.com!not-for-mail
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vb:149116
| X-Tomcat-NG: microsoft.public.dotnet.languages.vb
|
| > I'm curious. Can I ask what you want to trap Shift-Tab for?
|
|
| Fergus,
|
| Thank you for the information. It really helps to understand what
| these properties are for rather than just dumping the code into my
| application and hoping for the best.
|
| To answer your question I am attempting to create foward and backward
| navigation in a datagrid that does not support these functions. When
| the press TAB it will navigate through the grid but when the user
| reached the last column in the row I wanted to put the focus on the
| first column in the next row and when the reach the last column in the
| last row and press TAB I wanted to add a row to continue data entry.
|
| The Shift+Tab was to allow the user to backup to the prior column or
| if they are in the first column then put the focus in the last column
| of the previous row.
|
| A little grid customization to make data entry a little easier for the
| user. However, they will have to 'click' off the control to get out.
|
| Tim Frawley
|

Nov 20 '05 #6

P: n/a
Hi Jeffrey,

I'm just curious as to why you go down as far as the message level (ie.
ProcessKeyPreview) where you need to know and define (potentially mysterious)
WM_ and VK_ constants, when the higher levels such as ProcessCmdKey, provide
..NET constants and Boolean properties, etc.

Regards,
Fergus
Nov 20 '05 #7

P: n/a

Hi Fergus,

I just provide Tim another way of handle key event as a supplement :)
Just for this given issue, I think your solution of overriding the
ProcessCmdKey method is more suitable, it gives a more simple way of
processing.(No need handle the message).

Btw: all these WM_ and VK_ constants can be retrieved from the VB6 tools
API viewer.

Thanks for your posting.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: "Fergus Cooney" <fi*****@post.com>
| References: <bf*************************@posting.google.com>
<ub**************@tk2msftngp13.phx.gbl>
<bf*************************@posting.google.com>
<E2**************@cpmsftngxa06.phx.gbl>
| Subject: Re: Anyone know how to capture a Shift+Tab in the KeyPress event?
| Date: Thu, 23 Oct 2003 03:34:18 +0100
| Lines: 11
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1106
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
| Message-ID: <ec**************@tk2msftngp13.phx.gbl>
| Newsgroups: microsoft.public.dotnet.languages.vb
| NNTP-Posting-Host: 217.137.80.22
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftn gp13.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vb:149325
| X-Tomcat-NG: microsoft.public.dotnet.languages.vb
|
| Hi Jeffrey,
|
| I'm just curious as to why you go down as far as the message level
(ie.
| ProcessKeyPreview) where you need to know and define (potentially
mysterious)
| WM_ and VK_ constants, when the higher levels such as ProcessCmdKey,
provide
| .NET constants and Boolean properties, etc.
|
| Regards,
| Fergus
|
|
|

Nov 20 '05 #8

P: n/a
Hi Jeffrey,

Thanks. :-)

Regards,
Fergus

Nov 20 '05 #9

P: n/a
Jeffrey,

Thank you for the information. You are right, the grid does normally
respond to the tab key as well as shift tab for navigation. I have
utilized your code example for the following parts of the grid
navigation that where not supported.

When the user is on the first row, first column of the datagrid and
they press Shift+Tab they will navigate to the control prior to the
grid. My code to allow tabbing out of the control when on the first
row, first col would have acted as a Tab because I could not properly
detect a Shift+Tab in the KeyPress event. Your override method gave
me the ability to test for that condition and handle it appropriately.

When the user is on the last row, last column of the grid and they
press Tab they will navigate to the next control rather than having to
hit CTRL+Tab. Also, if the user is on the last row, last column if
they hit Shift+Tab they will navigate backwards. My code to allow
tabbing out of the control when on the last row, last col would have
jumped them to the next control because I could not properly detect
the Shift+Tab in the KeyPress event. Your override method gave me the
ability to test for that condition and handle it appropriately.

Other than these override conditions for tabbing in and out of the
control the normal navigation of the grid is left 'as is'.

Your help is much appreciated. Thank you!

And to everyone who posted on this request, thank you! Your insights
have been very useful!

Sincerely,

Tim Frawley
Nov 20 '05 #10

P: n/a

Hi Tim,

I am glad my sample code makes sence to you.
I also took notice of the datagrid's default navigation behavior in the
first column first row and last column last row.
In my personal feeling, I think your new way of design feels more
comfortable :)
Anyhow, your program finally works, enjoy it.

Have a nice day.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: ti*********@fishgame.state.ak.us (Tim Frawley)
| Newsgroups: microsoft.public.dotnet.languages.vb
| Subject: Re: Anyone know how to capture a Shift+Tab in the KeyPress event?
| Date: 23 Oct 2003 12:46:55 -0700
| Organization: http://groups.google.com
| Lines: 34
| Message-ID: <bf************************@posting.google.com>
| References: <bf*************************@posting.google.com>
<Jv**************@cpmsftngxa06.phx.gbl>
| NNTP-Posting-Host: 146.63.252.53
| Content-Type: text/plain; charset=ISO-8859-1
| Content-Transfer-Encoding: 8bit
| X-Trace: posting.google.com 1066938416 25993 127.0.0.1 (23 Oct 2003
19:46:56 GMT)
| X-Complaints-To: gr**********@google.com
| NNTP-Posting-Date: Thu, 23 Oct 2003 19:46:56 +0000 (UTC)
| Path:
cpmsftngxa06.phx.gbl!cpmsftngxa09.phx.gbl!TK2MSFTN GP08.phx.gbl!newsfeed00.su
l.t-online.de!t-online.de!diablo.theplanet.net!news.maxwell.syr.ed u!postnews
1.google.com!not-for-mail
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vb:149657
| X-Tomcat-NG: microsoft.public.dotnet.languages.vb
|
| Jeffrey,
|
| Thank you for the information. You are right, the grid does normally
| respond to the tab key as well as shift tab for navigation. I have
| utilized your code example for the following parts of the grid
| navigation that where not supported.
|
| When the user is on the first row, first column of the datagrid and
| they press Shift+Tab they will navigate to the control prior to the
| grid. My code to allow tabbing out of the control when on the first
| row, first col would have acted as a Tab because I could not properly
| detect a Shift+Tab in the KeyPress event. Your override method gave
| me the ability to test for that condition and handle it appropriately.
|
| When the user is on the last row, last column of the grid and they
| press Tab they will navigate to the next control rather than having to
| hit CTRL+Tab. Also, if the user is on the last row, last column if
| they hit Shift+Tab they will navigate backwards. My code to allow
| tabbing out of the control when on the last row, last col would have
| jumped them to the next control because I could not properly detect
| the Shift+Tab in the KeyPress event. Your override method gave me the
| ability to test for that condition and handle it appropriately.
|
| Other than these override conditions for tabbing in and out of the
| control the normal navigation of the grid is left 'as is'.
|
| Your help is much appreciated. Thank you!
|
| And to everyone who posted on this request, thank you! Your insights
| have been very useful!
|
| Sincerely,
|
| Tim Frawley
|

Nov 20 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.