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

identify mouse click vs keyboard shortcut on menu item

我在 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

您可以处理 ProcessCmdKey 表单并检查快捷方式,如果按下了快捷方式,return 为真。这样点击只会通过点击而不是通过快捷方式提升:

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