0

Is there anyway to check if a menu item event is coming from a click in the menu or from a short cut key being pressed?

I've tried adding event handlers to the key press and key down events, however these events aren't being "fired" when it's a shortcut key that's pressed (They do work as expected when it's not a shortcut key). I couldn't find anything in the sender object that was different between the menu click or shortcut click.

John Arlen
  • 6,539
  • 2
  • 33
  • 42
GraeningM
  • 78
  • 7
  • 1
    Just out of curiosity, what's the point of differentiating click from key shortcut? – ken2k Jan 09 '13 at 16:02
  • 1
    May I ask why you need such functionality? Normally I'd say that there should not be any difference between mouse click or key press, it is one task of the Menu Item to abstract from that. – Desty Jan 09 '13 at 16:03
  • One computer is having a problem where it's seemingly randomly opening a form in the application that's tied to a F3 key. My suspicion is that the computer's keyboard is sending the key. However, since there are a handful of ways to open the form (Menu click, short cut, a drop down of "recently opened" forms, or just closing the form on top and going back to it) and the problem has never happened when I'm around I can't verify the problem. So I wanted to log when the F3 key was being pressed and see if it matched when the problem is coming up. – GraeningM Jan 10 '13 at 15:50

4 Answers4

1

Well to get help, you should post what you have tried. (Your source)

You can use a enum for this:

enum Sender
{
    Shortcut,
    Menu
}

void MenuEvent(Sender sender)
{
    if (sender == Sender.Shortcut)
    {

    }
    else
    {

    }
}

//if you click from the menu
void btnMenuClick()
{
    MenuEvent(Sender.Menu);
}

//if you use shortcut
void OnShortcutEvent()
{
    MenuEvent(Sender.Shortcut);
}

Edit: I guess my answer was to vague so I edited the code. I hope its more clear now, but I must say the OP should give more details as well, such as posting some code.

joell
  • 396
  • 6
  • 17
  • 2
    to say you the true, I do not understand your solution – platon Jan 09 '13 at 16:11
  • @platon I'm sorry I hope its clear now, let me know! But the OP could also be a bit more clear as well. – joell Jan 09 '13 at 17:12
  • I'm not sure which code you want, what I'm using to create the menu? I can add it, but it's significantly more complicated than need be since it's creating a menu based on information kept in a database so we can control who gets what menu items. The core is fairly simple, just a menu item with a short cut and an event on the on click of the menu item – GraeningM Jan 10 '13 at 15:55
  • @GraeningM You could post the code what triggers the event? At least for the shortcut it could be possible to adapt this solution, and if its not that one you can assume it was from the menu. – joell Jan 10 '13 at 16:00
1

You can catch all key combinations by overriding ProcessCmdKey:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData) 
{
    if (keyData == (Keys.Control | Keys.F)) 
    {
        Console.WriteLine("My code ran from shortcut");
        myFunction();
    }
    return base.ProcessCmdKey(ref msg, keyData);
}

private void ToolStripMenuItem_click(object sender ...)
{
  Console.WriteLine("My code ran from menu item");
  myFunction();
}

void myFunction()
{
  //your functionality here
}
VladL
  • 12,769
  • 10
  • 63
  • 83
  • Thanks, the ProcessCmdKey is what I wanted. Just to note, that if it's a menu click or a shotcut key the ToolStripMenuItem_Click event is going to be raised. – GraeningM Jan 10 '13 at 15:59
0

I see a single solution to this problem - override the ToolStripMenuItem's ProcessCmdKey method which is raised when a shortcut is processed. In this case, you can determine when a click was caused by a shortcut. Obviously, you need to use your own class of the ToolstripMenuItem instead of the standard one.

platon
  • 5,310
  • 1
  • 22
  • 24
0

Handle the MouseDown event to process your mouse-click.

menuItem.MouseDown += new MouseEventHandler(Process_Mouse_Click_Handler);

Handle the Click event to process your shortcut.

menuItem.Click+= new EventHandler(Process_Shortcut_Handler);

  • If you go this route (and i'm not advocating you do) then you should handle the mouse up event instead. – Chad Jun 09 '17 at 20:23