PreviewKeyDown只触发一次

时间:2013-07-08 04:32:30

标签: vb.net visual-studio-2010

在visual basic 2010 express中,我最初使用keyDown事件来使用箭头来控制图片框,但只有在按住ctrl + arrowkey时才会触发..而我在网上找到的解决方案是使用previewKeyDown事件。这只会触发一次,然后根本不响应任何按键。 KeyPreview已设置为true。

  Private Sub Form1_PreviewKeyDown(ByVal sender As System.Object, ByVal e As    System.Windows.Forms.PreviewKeyDownEventArgs) Handles MyBase.PreviewKeyDown
    Select Case e.KeyCode
        Case Keys.Up
            vy = vy - 2
        Case Keys.Left
            vx = vx - 2
        Case Keys.Right
            vx = vx + 2
    End Select
  End Sub

2 个答案:

答案 0 :(得分:0)

更改

Private Sub Form1_PreviewKeyDown(ByVal sender As System.Object, ByVal e As    System.Windows.Forms.PreviewKeyDownEventArgs) Handles MyBase.PreviewKeyDown

Private Sub Form1_PreviewKeyDown(ByVal sender As System.Object, ByVal e As    System.Windows.Forms.PreviewKeyDownEventArgs) Handles Me.PreviewKeyDown

还尝试添加e.Handled=True

Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    Select Case e.KeyCode
        Case Keys.Up
            vy = vy - 2
            e.Handled = True
        Case Keys.Left
            vx = vx - 2
            e.Handled = True
        Case Keys.Right
            vx = vx + 2
            e.Handled = True
    End Select
End Sub

答案 1 :(得分:0)

尝试使用KeyDown而不是PreviewKeyDown,如下所示:

Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    Select Case e.KeyCode
        Case Keys.Up
            vy = vy - 2 
        Case Keys.Left
            vx = vx - 2
        Case Keys.Right
            vx = vx + 2                       
    End Select
End Sub