vb.net Visual Basic 中的 KeyPress 事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20944182/
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
KeyPress Event in Visual Basic?
提问by Meatie
I think this is possible, but I don't know... I want to check when the key A is pressed to move the player to the left, but for now just a messagebox.
我认为这是可能的,但我不知道......我想检查按下 A 键将玩家向左移动的时间,但现在只是一个消息框。
Here is all the code I could find on the internet, it didn't work...
这是我在互联网上找到的所有代码,它不起作用...
Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
If e.KeyCode = Keys.A Then
MsgBox("Left")
End If
End Sub
I am not asking how to do things when you enter stuff in a textbox, I'm asking how to run a event when you press a key.
我不是问当你在文本框中输入内容时如何做事,我问的是当你按下一个键时如何运行一个事件。
回答by chris_techno25
Try this...
尝试这个...
Private Sub TextBox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If Asc(e.KeyChar) = 97 Or Asc(e.KeyChar) = 65 Then
MsgBox("hello")
End If
End Sub
This should work. 97 is 'a' and 65 is 'A' in ASCII.
这应该有效。97 是 'a',65 是 ASCII 中的 'A'。
回答by Vignesh Kumar A
Try this
尝试这个
Private Sub TextBox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyCode = Keys.A Then
MsgBox("Left")
End Sub
or
或者
Private Sub textBox1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) _
Handles textBox1.KeyDown
If e.KeyCode = Keys.A Then
MsgBox("Left")
end if
End Sub
回答by Nagaraj S
- You probably want to set the form's KeyPreview property to true. Doing this makes sure the form sees all key events even if one of its child controls
- Use the KeyEventArgs.KeyData property to see what key was pressed.
KeyCode , KeyData and KeyValue
are members ofSystem.Windows.Forms.KeyEventArgs
in the KeyUp and KeyDown events only.
- 您可能希望将表单的 KeyPreview 属性设置为 true。这样做可以确保表单看到所有关键事件,即使它的子控件之一
- 使用 KeyEventArgs.KeyData 属性查看按下了什么键。
KeyCode , KeyData and KeyValue
仅System.Windows.Forms.KeyEventArgs
是 KeyUp 和 KeyDown 事件中的成员 。
回答by Jared Horsch
They actually simplified this from VB6.The new way with VB 2017 is...
他们实际上从 VB6 简化了这一点。 VB 2017 的新方法是......
Private Sub Textbox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtProblem.KeyPress
If e.KeyCode = "a" or e.KeyCode = "A" Then
MsgBox("Left")
End If
End Sub
回答by user9340952
So, I got one step in the right direction maybe it will help. I'm trying to give control of the paddle to the player in a game of pong and found if textbox1 is highlighted or "has focus" then these commands will execute and I can move the paddle around the screen. The problem is there is also a textbox filling up with letters in the corner of the screen as this happens I'm not sure how to get rid of just yet. Perhaps it will be useful though.
所以,我朝着正确的方向迈出了一步,也许它会有所帮助。我试图在乒乓球游戏中将桨的控制权交给玩家,发现如果 textbox1 突出显示或“具有焦点”,那么这些命令将执行,我可以在屏幕上移动桨。问题是还有一个文本框在屏幕的角落里填满了字母,因为发生这种情况我不知道如何摆脱。也许它会很有用。
Private Sub
私人潜艇
Textbox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If Asc(e.KeyChar) = 97 Then 'a
Paddle.Location = New Point(Paddle.Location.X - 10, Paddle.Location.Y)
End If
If Asc(e.KeyChar) = 119 Then 'w
Paddle.Location = New Point(Paddle.Location.X, Paddle.Location.Y - 10)
End If
If Asc(e.KeyChar) = 100 Then 'd
Paddle.Location = New Point(Paddle.Location.X + 10, Paddle.Location.Y)
End If
If Asc(e.KeyChar) = 115 Then 's
Paddle.Location = New Point(Paddle.Location.X, Paddle.Location.Y + 10)
End If
End Sub
Also, you can use this command to give focus to the textbox from a different event
此外,您可以使用此命令将焦点放在不同事件的文本框上
Public Sub ControlSetFocus(control As Control)
If Control.CanFocus Then
control.Focus()
End If
End Sub
回答by ARN
This works for me
这对我有用
If e.KeyChar = Convert.ToChar("a") Then
MsgBox(Convert.ToChar("a") +"enter key pressed ")
End If
Also you can use numbers and uppercase in ""
您也可以在“”中使用数字和大写