user541686
user541686

Reputation: 210755

How to detect if the Control.Click event was by the mouse, keyboard, or something else?

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)

Upvotes: 3

Views: 2602

Answers (2)

Joe
Joe

Reputation: 82654

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;
}

Upvotes: 4

shf301
shf301

Reputation: 31404

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.

Upvotes: 6

Related Questions