开发者

Visual Basic Key Listener

开发者 https://www.devze.com 2023-03-31 00:01 出处:网络
I am trying to wr开发者_如何学运维ite a program in Visual Basic (with VS 2010) that will respond to the arrow keys. I am aware that there are key listener in Java but not sure if such thing exist in V

I am trying to wr开发者_如何学运维ite a program in Visual Basic (with VS 2010) that will respond to the arrow keys. I am aware that there are key listener in Java but not sure if such thing exist in VB and how to code it. Please show me some example on this. Thanks.


If you are doing winforms then set the KeyPreview property of the form to true and then set the KeyDown event. Your code will be like this:

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
0

精彩评论

暂无评论...
验证码 换一张
取 消