在菜单项上识别鼠标单击与键盘快捷键

时间:2015-12-08 18:40:22

标签: vb.net winforms menuitem

我在vb.net 2010上。在我的winforms应用程序中,我有一个menuItem单击事件处理程序,可以通过鼠标单击或键盘快捷键调用。如何通过鼠标单击与键盘快捷键

调用我的处理程序
Private Sub myToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles myMenuItemClick
   Dim MouseClicked As Boolean 
   MouseClicked = ?? how to make the distinction how handler was called ?? 
   If MouseClicked Then
      lblClicked.Text = "Mouse Click" 
   Else
      lblClicked.Text = "Key board shortcut" 
   End If
End Sub

1 个答案:

答案 0 :(得分:1)

您可以处理ProcessCmdKey表单并检查快捷方式,如果按下了快捷方式,则返回true。这样,点击仅通过点击而不是通过快捷方式提升:

Private Sub NewToolStripMenuItem_Click(sender As Object, e As EventArgs) _
    Handles NewToolStripMenuItem.Click

    MessageBox.Show("Click")
End Sub

Protected Overrides Function ProcessCmdKey(ByRef msg As Message, keyData As Keys) _
    As Boolean

    If (Me.NewToolStripMenuItem.ShortcutKeys = keyData) Then
        MessageBox.Show("ShortCut")
        Return True
    End If
    Return MyBase.ProcessCmdKey(msg, keyData)
End Function