【发布时间】:2011-08-27 16:17:49
【问题描述】:
我正在尝试用 Visual Basic(使用 VS 2010)编写一个响应箭头键的程序。我知道 Java 中有关键监听器,但不确定 VB 中是否存在这样的东西以及如何对其进行编码。请给我看一些这方面的例子。 谢谢。
【问题讨论】:
标签: vb.net keylistener
我正在尝试用 Visual Basic(使用 VS 2010)编写一个响应箭头键的程序。我知道 Java 中有关键监听器,但不确定 VB 中是否存在这样的东西以及如何对其进行编码。请给我看一些这方面的例子。 谢谢。
【问题讨论】:
标签: vb.net keylistener
如果你在做winforms,那么将表单的KeyPreview 属性设置为true,然后设置KeyDown 事件。您的代码将如下所示:
Dim previousKey As Keys? = Nothing
Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.Up Then
'up arrow
End If
If e.KeyCode = Keys.Left Then
'left arrow
If Not previousKey Is Nothing And previousKey = Keys.Up Then
'Up arrow and then Left Arrow
MessageBox.Show("there, that's better")
End If
End If
If e.KeyCode = Keys.Right Then
'right arrow
End If
If e.KeyCode = Keys.Down Then
'down arrow
End If
'After everything is done set the current key as the previous key.
previousKey = e.KeyCode
End Sub
【讨论】: