vb.net 组合键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4442805/
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
vb.net key combination
提问by Elliott
I'm trying to capture two key presses in my VB.net application, for this example CTRL + B, the code below doesn't work but it does for single keys. I have tried setting keypreview as true but this has no effect.
我试图在我的 VB.net 应用程序中捕获两次按键,对于这个例子 CTRL + B,下面的代码不起作用,但它适用于单个键。我试过将 keypreview 设置为 true 但这没有效果。
Private Sub main_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles mybase.KeyDown
If e.KeyCode = Keys.ControlKey And e.KeyCode = Keys.B Then
MsgBox("CTRL + B Pressed !")
End If
End Sub
End Class
Thanks
谢谢
回答by Stuart Thompson
The Control key is a Modifier key. This code tests for Ctrl + B
Control 键是一个修饰键。此代码测试 Ctrl + B
e.KeyCode = Keys.B AndAlso e.Modifiers = Keys.Control
The key-code is B, but the modifier is Ctrl.
键码是 B,但修饰符是 Ctrl。
Your code snippet, updated:
您的代码片段已更新:
Private Sub main_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles mybase.KeyDown
If (e.KeyCode = Keys.B AndAlso e.Modifiers = Keys.Control) Then
MsgBox("CTRL + B Pressed !")
End If
End Sub
回答by Nitin Sangwan
You need to add controlkey also after modifier to make it work properly.
您还需要在修饰符之后添加 controlkey 以使其正常工作。
Private Sub main_KeyDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles mybase.KeyDown
If (e.KeyCode = Keys.B AndAlso e.Modifiers.ControlKey = Keys.Control) Then
MsgBox("CTRL + B Pressed !")
End If
End Sub
回答by Rich Turner
If you look at the documentation for KeyEventArgs, you'll note that the class exposes properties for ALT, CTRL and Modifiers which allow you to determine whether these keys were pressed in addition to the main symbol key you're interested in.
如果您查看KeyEventArgs的文档,您会注意到该类公开了 ALT、CTRL 和 Modifiers 的属性,这些属性允许您确定除了您感兴趣的主符号键之外是否按下了这些键。
Private Sub main_KeyDown(
ByVal sender As Object,
ByVal e As System.Windows.Forms.KeyEventArgs)
Handles mybase.KeyDown
If e.Control And e.KeyCode = Keys.B Then
MsgBox("CTRL + B Pressed !")
End If
End Sub