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

匆匆过客 提交于 2019-12-22 10:59:45

问题


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


来源:https://stackoverflow.com/questions/6645387/how-to-detect-if-the-control-click-event-was-by-the-mouse-keyboard-or-somethin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!