任何人都知道如何在KeyPress事件中捕获Shift + Tab? [英] Anyone know how to capture a Shift+Tab in the KeyPress event?

查看:96
本文介绍了任何人都知道如何在KeyPress事件中捕获Shift + Tab?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检测KeyPress事件中的Shift + Tab,以便在不支持此方法的控件上返回

导航。有没有人

有任何想法如何比较e.KeyChar和ShiftTab?


Tim

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

推荐答案

嗨Tim,


有关键盘处理的一些信息:


KeyPress用于可键入的字符键 - 因此它'KeyChar作为Char。

对于一般的键,你会使用KeyDown。


KeyDown有一个KeyEventArgs,可以让你使用Alt,Control和Shift -

布尔属性 - 单独测试shift键,KeyCode和


KeyCode将是关键代码,Keydata是相同的键结合

shift-key信息。然后,为了给你更多的选择,有

修改器,它只为你提供了一个移位密钥信息,组合成一个

的财产。


请记住KeyDown会在Shift时出现一次,然后再次出现在被转移的

键上。


Sub KeyPress(..,e As KeyEventData)

如果e.Shift和e.KeyCode = Keys.T那么

''我们有一个资金T 。


但是!!


说到某些键,比如Tab和Alt-F(当有菜单时)带有热键的
项目,例如& File),甚至在KeyDown获得

之前就会对它们进行处理。所以要到达<这些>你必须先把钥匙挂进钥匙

处理顺序。


有一大堆处理密钥的方法。你想要的那个,是
ProcessCmdKey,用于处理热键。你可以覆盖这个并且

测试Tab是否被按下了。如果是这样,你可以自己处理它。

完成后,你传回True表示你不想要默认的

行为。如果你不处理密钥,你应该允许你的基类

这样做。


我很好奇。我可以问一下你想要为Shift-Tab捕获什么?


问候,

Fergus


<代码>

受保护的覆盖函数ProcessCmdKey(ByRef msg As Message,_

ByVal keyData As Keys)

As Boolean

如果KeyData =(Keys.Shift + Keys.Tab)那么

''现在我们正在做点什么!

:::

返回True''阻止正常的Shift-Tab行为

结束如果

返回MyBase.ProcessCmdKey''其他键正常工作。

结束功能

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




你好,


您可以覆盖ProcessKeyPreview方法,然后处理WM_KEYUP

消息。

示例代码如下:


公共Const WM_KEYUP =& H101

Public Const VK_TAB =& H9


受保护的覆盖函数ProcessKeyPreview(ByRef m As Message)As

布尔什

如果是m.Ms g = WM_KEYUP然后

如果m.WParam.ToInt32()= VK_TAB那么

如果Control.ModifierKeys = Keys.Shift那么

MessageBox。显示(Shift + Tab)

结束如果

结束如果

结束如果

MyBase.ProcessKeyPreview (m)

结束功能


它在我的机器上运行良好,如果你有任何不清楚的地方,请随意

到让我知道。


祝你好运,

Jeffrey Tan

微软在线合作伙伴支持

获取安全! - www.microsoft.com/security

此帖子按原样提供没有保证,也没有赋予任何权利。


--------------------

|来自: ti ********* @ fishgame.state.ak.us (Tim Frawley)

|新闻组:microsoft.public.dotnet.languages.vb

|主题:任何人都知道如何在KeyPress事件中捕获Shift + Tab?

|日期:2003年10月21日17:42:27 -0700

|组织: http://groups.google.com

|行数:5

|消息ID:< bf ************************* @ posting.google.com>

| NNTP-Posting-Host:146.63.252.53

|内容类型:text / plain; charset = ISO-8859-1

|内容传输编码:8位

| X-Trace:posting.google.com 1066783347 5456 127.0.0.1(2003年10月22日
$ b $ 00 00:42:27 GMT)

| X-Complaints-To: gr**********@google.com

| NNTP-Posting-Date:2003年10月22日星期三00:42:27 +0000(UTC)

|路径:

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

|外翻:cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vb:148908

| X-Tomcat-NG:microsoft.public.dotnet.languages.vb

|

|我试图在KeyPress事件中检测到Shift + Tab,返回

|在不支持此方法的控件上导航。有没有人

|有任何想法如何比较e.KeyChar和ShiftTab?

|

|蒂姆

|


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
|


>我很好奇。我可以问一下你想要为Shift-Tab捕获什么?

Fergus,


感谢您提供相关信息。了解这些属性的用途真的很有帮助,而不仅仅是将代码转储到我的

应用程序中,并希望能够做到最好。


为了回答你的问题,我试图在不支持这些功能的数据网格中创建foward和back

导航。按

按TAB键将导航通过网格,但当用户

到达行中的最后一列时,我想把重点放在

下一行的第一列,当到达最后一行

最后一行并按TAB时,我想添加一行以继续输入数据。


Shift + Tab允许用户备份到前一列或

如果它们在第一列,然后将焦点放在最后一列
$ b $上一行的b。


一个小网格自定义,使

用户的数据输入更容易一些。但是,他们必须点击掉控制才能离开。


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


这篇关于任何人都知道如何在KeyPress事件中捕获Shift + Tab?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆