vb.net组合键

时间:2010-12-14 18:31:46

标签: vb.net

我正在尝试在我的VB.net应用程序中捕获两个按键,对于此示例CTRL + B,下面的代码不起作用,但它适用于单个键。我已经尝试将keypreview设置为true,但这没有效果。

  Private Sub main_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles mybase.KeyDown
            If e.KeyCode = Keys.ControlKey And e.KeyCode = Keys.B Then
                MsgBox("CTRL + B Pressed !")
            End If
        End Sub
    End Class

由于

4 个答案:

答案 0 :(得分:16)

Control键是修饰键。此代码测试Ctrl + B

e.KeyCode = Keys.B AndAlso e.Modifiers = Keys.Control

键码为B,但修饰符为Ctrl。

您的代码段已更新:

Private Sub main_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles mybase.KeyDown
    If (e.KeyCode = Keys.B AndAlso e.Modifiers = Keys.Control) Then
        MsgBox("CTRL + B Pressed !")
    End If
End Sub

答案 1 :(得分:2)

您还需要在修改后添加controlkey才能使其正常工作。

Private Sub main_KeyDown(ByVal sender As Object, ByVal e As
  System.Windows.Forms.KeyEventArgs) Handles mybase.KeyDown
    If (e.KeyCode = Keys.B AndAlso e.Modifiers.ControlKey = Keys.Control) Then
        MsgBox("CTRL + B Pressed !")
    End If
End Sub

答案 2 :(得分:1)

如果查看documentation for KeyEventArgs,您会注意到该类公开ALT,CTRL和Modifiers的属性,这些属性允许您确定除了您感兴趣的主符号键之外是否按下了这些键in。

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

    If e.Control And e.KeyCode = Keys.B Then
        MsgBox("CTRL + B Pressed !")
    End If
End Sub

答案 3 :(得分:0)

你可以使用这个:

currentpix