vb.net 如何在visual basic中的keyDown事件中找出按下了哪个键?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14355966/
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 to find out which key is pressed in keyDown event in visual basic?
提问by Navid777
I'm writing my first visual basic program, And I want to do something when for example the 'k' button is pressed, I know that I should write the code in "KeyDown" event, but I don't know how to find out that 'k' button is pressed or not
我正在编写我的第一个可视化基本程序,我想在例如按下“k”按钮时做一些事情,我知道我应该在“KeyDown”事件中编写代码,但我不知道如何找到确定是否按下了“k”按钮
采纳答案by SysDragon
If you are using a Windows Forms Application, you have to put the KeyPreviewproperty of the form to Trueso the form will monitorize key events.
如果您使用的是 Windows 窗体应用程序,则必须将窗体的KeyPreview属性放入,True以便窗体监视关键事件。
Then:
然后:
Private Sub Form1_KeyPress(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
If e.KeyChar = "k" OrElse e.KeyChar = "K" Then
MessageBox.Show("Pressed!")
End If
End Sub
If you prefer, you could use other event:
如果您愿意,可以使用其他事件:
Private Sub Form1_KeyDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.K Then
MessageBox.Show("Pressed!")
End If
End Sub

