vb.net 如何在 VB 中按下按键触发事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23176009/
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 I Trigger An Event on the press of a key in VB?
提问by Feyisayo Sonubi
I have an app in visual basic that displays a message box with a text range from A - F upon user integer input in a textbox.
我有一个 Visual Basic 应用程序,当用户在文本框中输入整数时,它会显示一个文本范围为 A - F 的消息框。
I have a button control on the form that triggers the event, but the user has to press the button every time to get a result, that's cool, but what if I wanted the event triggered by say the Bkey on my keyboard?
我在表单上有一个触发事件的按钮控件,但是用户每次都必须按下按钮才能获得结果,这很酷,但是如果我希望通过B键盘上的键触发事件怎么办?
I know of a way of assigning the AcceptButtonproperty to my button in visual studio 2012 which accepts just the Enterkey alone. I want the same effect as pressing the button on the press of a key on a keyboard instead. Thanks.
我知道一种将AcceptButton属性分配给我在 Visual Studio 2012 中的按钮的方法,它只接受Enter键。我想要与按下键盘上的按键时按下按钮相同的效果。谢谢。
采纳答案by xpda
Use the form Keydown event, and check to see if the B key was pressed. If so, display the messagebox.
使用表单 Keydown 事件,并检查是否按下了 B 键。如果是,则显示消息框。
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.B Then MsgBox("B key was pressed")
End Sub

