Vb.net Visual Basic密钥侦听器

Vb.net Visual Basic密钥侦听器,vb.net,keylistener,Vb.net,Keylistener,我正在尝试用VisualBasic(VS2010)编写一个程序,该程序将响应箭头键。我知道Java中有键侦听器,但不确定VB中是否存在这样的东西,以及如何对其进行编码。请给我举一些例子。 谢谢。如果您正在使用winforms,请将表单的KeyPreview属性设置为true,然后设置KeyDown事件。您的代码如下所示: Dim previousKey As Keys? = Nothing Private Sub Form1_KeyDown(ByVal sender As System.Obj

我正在尝试用VisualBasic(VS2010)编写一个程序,该程序将响应箭头键。我知道Java中有键侦听器,但不确定VB中是否存在这样的东西,以及如何对其进行编码。请给我举一些例子。
谢谢。

如果您正在使用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

谢谢。这就是我想要的。现在,当按下2个键时,是否可以让程序响应?比如当按下向上箭头键和向左箭头键时?我修改了我的示例以捕获向上箭头,然后是向左箭头。有可能使其行为像赛车游戏一样吗?嗯,赛车游戏存在,所以我认为这是可能的。你需要创建一个新的帖子,虽然你有具体的问题,你已经尝试解决它。