How to detect if the Control.Click event was by th

2019-05-13 19:27发布

问题:

How can I tell if a Control.Click event was triggered by the mouse or by the keyboard?

Edit:

Handling MouseClick and KeyPress does't work for me, because then how would I know if something else triggered the click? (e.g. PerformClick)

回答1:

You can't. Use the Control.MouseClick event and the Control.KeyPress event so you can tell the source of the event. And remember that a space on the control with focus and a Ctrl+ key can generate a click on a button as well.



回答2:

You can not tell, but you can use MouseClick and KeyPress if you need to know what originated the event.

void handler(object sender, EventArgs e)
{
    bool mouseEvent = (e is MouseEventArgs);
    bool keyEvent = (e is KeyEventArgs);
    bool performClick = (e is EventArgs) && !keyEvent && !mouseEvent;
}