Keydown事件奇怪的行为

时间:2013-04-11 13:15:26

标签: vb.net events keydown

我正在尝试捕捉快捷键。我需要解释如何管理KeyDown Events。我们以此为例:

Private Sub SoldeOuvertFou_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown

    If e.Control And e.KeyCode = Keys.W Then
        MessageBox.Show("Ctrl+W")
        End If

    If e.Control And e.KeyCode = Keys.F5 Then
          MessageBox.Show("Ctrl+F5")
        End If

  End Sub

完美无缺。无论我先按哪一个或按下它们多少次,MessageBox都会弹出。现在如果我只是改变子中的顺序:

Private Sub SoldeOuvertFou_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown

    If e.Control And e.KeyCode = Keys.F5 Then
          MessageBox.Show("Ctrl+F5")
        End If

    If e.Control And e.KeyCode = Keys.W Then
        MessageBox.Show("Ctrl+W")
        End If

  End Sub

使用此方法,只会弹出Ctrl+F5。没办法让Ctrl+W出现......有什么想法吗?

1 个答案:

答案 0 :(得分:0)

你可以简单地使用:

If e.Control Then

    If e.KeyCode = Keys.F5 Then
        MessageBox.Show("Ctrl+F5")
    Else
        If e.KeyCode = Keys.W Then
           MessageBox.Show("Ctrl+W")
        End If

    End If

End If