你如何在 VB.NET 中检测同时按下的按键,例如“Ctrl + T”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13803761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you detect simultaneous keypresses such as "Ctrl + T" in VB.NET?
提问by J2Tuner
I am trying to detect the keys "Control" and "t" being pressed simultaneously in VB.NET. The code I have so far is as follows:
我试图检测在 VB.NET 中同时按下的“Control”和“t”键。我到目前为止的代码如下:
Private Sub frmTimingP2P_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyValue = Keys.ControlKey And e.KeyValue = Keys.T Then
MessageBox.Show("Ctrl + T")
End If
End Sub
I can detect one key or the other by removing the and statement and the second keyvalue statement, but I don't really get anything when I try this. Is there another method?
我可以通过删除 and 语句和第二个 keyvalue 语句来检测一个或另一个键,但是当我尝试这个时我没有真正得到任何东西。还有其他方法吗?
Thanks
谢谢
采纳答案by Konrad Rudolph
First of all, And
in your code should be AndAlso
since it's a logical operator. And
in VB is a bit operator. Next, you can use the Modifiers
propertyto test for modifier keys:
首先,And
在你的代码中应该是AndAlso
因为它是一个逻辑运算符。And
在VB中是位运算符。接下来,您可以使用该Modifiers
属性来测试修饰键:
If (e.KeyCode And Not Keys.Modifiers) = Keys.T AndAlso e.Modifiers = Keys.Ctrl Then
MessageBox.Show("Ctrl + T")
End If
The e.KeyCode And Not Keys.Modifiers
in the first part of the condition is necessary to mask out the modifier key.
的e.KeyCode And Not Keys.Modifiers
掩盖了修饰键中的条件的第一部分是必要的。
If e.Modifiers = Keys.Ctrl
can also be written as If e.Control
.
If e.Modifiers = Keys.Ctrl
也可以写成If e.Control
。
Alternatively, we can collate these two queries by asking directly whether the combination Ctrl+Twas pressed:
或者,我们可以通过直接询问是否按下了组合Ctrl+来整理这两个查询T:
If e.KeyCode = (Keys.T Or Keys.Ctrl) Then …
In both snippets we make use of bit masks.
在这两个片段中,我们都使用了位掩码。
回答by Kuromuro
Private Sub frmMain_Zaporka_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
Private Sub frmMain_Zaporka_KeyDown(sender As Object, e As KeyEventArgs) 处理 MyBase.KeyDown
Select Case e.KeyData
Case (Keys.Control + Keys.Shift + Keys.F12)
MsgBox("Control + Shift + F12")
Case (Keys.Escape)
Me.Close()
End Select
' or
If e.KeyCode = Keys.F12 AndAlso e.Modifiers = (Keys.Control Or Keys.Shift) Then
MsgBox("Control + Shift + F12")
ElseIf e.KeyCode = Keys.Escape Then
Me.Close()
End If
' or
Select Case e.KeyCode
Case (Keys.F12 And e.Control And e.Shift)
MsgBox("Control + Shift + F12")
Case (Keys.Escape)
Me.Close()
End Select
End Sub
结束子
回答by user2348797
I had the same problem, but for me to get this to work I had to set the forms KeyPreviewproperty to true. In Visual studio you can change this in the Forms [Design] Property window or changing the property on load.
我遇到了同样的问题,但为了让这个工作正常,我必须将表单KeyPreview属性设置为true。在 Visual Studio 中,您可以在 Forms [Design] 属性窗口中更改此设置或在加载时更改属性。
Private Sub frmTimingP2P_Load(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles MyBase.Load
Me.KeyPreview = True
End Sub
then use by using:
然后使用:
Private Sub frmTimingP2P_KeyDown(ByVal Sender As Object, ByVal e As _
System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If (e.KeyCode = Keys.T AndAlso e.Modifiers = Keys.Control) Then
MessageBox.Show("Ctrl + T")
End If
End Sub
or other program logic as provided in the answers above.
或上述答案中提供的其他程序逻辑。
回答by Gerard Balaoro
I'll save you from the long code. Here:
我会把你从长代码中拯救出来。这里:
If e.Control And e.Alt And e.KeyCode = Keys.G Then
MsgBox("Control Alt G")
End If
回答by Ruben_PH
I dont have vb.net installed right now but try this on your keydown or keypress event:
我现在没有安装 vb.net,但在你的 keydown 或 keypress 事件上试试这个:
If e.KeyCode = Keys.T AndAlso e.Control = True Then
MsgBox("Ctrl + T")
End If