我正在尝试在Visual Basic中编写一个程序(使用VS 2010),它将响应箭头键。我知道Java中有关键的监听器,但不确定VB中是否存在这样的东西以及如何对其进行编码。请告诉我一些关于此的例子。 感谢。
答案 0 :(得分:2)
如果您正在执行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