473,376 Members | 1,009 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,376 software developers and data experts.

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

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
10 27751
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

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
> 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
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

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
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

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
Hi Jeffrey,

Thanks. :-)

Regards,
Fergus

Nov 20 '05 #9
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Norton | last post by:
Dear all, i am in a deadloop now... when i update the tabcontrol in selectedchanged event the appz will trigger another selectedchanged event and it never stop but i forgot the code to...
6
by: datactrl | last post by:
Hi, all Is that possible to fire a "tab" key press event with javascript? jack
0
by: Michael Howes | last post by:
I'm trying to handle a number of keystrokes in a TextBox and for other reasons I'm using the KeyDown I'm trying to know when Shift-Tab is pressed and the following code doesn't work if ( (...
2
by: orekinbck | last post by:
Hi There In a C# windows app in .NET 2003, I would like to capture when the user is within a certain text box and holds down shift then presses tab. Atm, my code is: protected override bool...
1
by: Long Le | last post by:
I checked to see if its wired correctly: private void InitializeComponent() { this.DataGrid1.PageIndexChanged += new...
0
by: Gene Hubert | last post by:
I've got this working but I've got to wonder if there's not a better way. I'm capturing the tab and shift-tab keys in a datagrid and doing custom processing. Also, the form designer gets...
0
by: LCH | last post by:
hi i would like to know how to capture a datagrid keypress event. I have form contains of a datagrid with 5 rows and 5 colums and a button. When i press on the enter button at the last row n...
0
by: naveennm | last post by:
Hi, In my application I have to check that if alt-tab is pressed then certain validation should not be done. But I dont know how to capture alt-tab key event. Please help me in this regard....
1
by: baburk | last post by:
Hi, I wants to capture browser tab close event? I got for window close event. if((window.event.clientX<0) || (window.event.clientY<0)) { }
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.