C#、WinForms:什么会阻止 KeyDown 事件从焦点控件链接到主窗体?只有叶子控件 KeyDown 对我有用
据我了解,当按下键盘按钮时,它应该为具有焦点的控件调用 KeyDown 事件。然后,父控件的 KeyDown,依此类推,直到到达主窗体。除非 - 沿着链中的一个事件处理程序做了:
e.SuppressKeyPress = true;
e.Handled = true;
在我的例子中,KeyDown 事件永远不会到达主窗体。 我有表格 ->面板->例如按钮。
Panel 不提供 KeyDown 事件,但它不应该阻止它到达主窗体,对吗?
现在,作为解决方案,我将每个控件设置为调用我编写的事件处理程序。我基本上是试图阻止 Alt-F4 关闭应用程序,而是将其最小化。
As i understand it, when a keyboard button is pressed it should invoke the KeyDown event for the control which has focus. Then, the KeyDown for the parent control, so on and so forth until it reaches main form. UNLESS - along the chain one of the EventHandlers did:
e.SuppressKeyPress = true;
e.Handled = true;
In my case, KeyDown events never get to the main form.
I have Form -> Panel -> button for example.
Panel doesn't offer a KeyDown Event, but it shouldn't stop it from reaching the main form right?
Right now as a work around I set every single control to call an event handler I wrote. I'm basically trying to prevent Alt-F4 from closing the application and instead minimize it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
[编辑]
如果您想捕获 Alt-F4,那么在控制级别尝试是没有意义的,因为击键是由应用程序处理的 - 请参阅 如何禁用 Alt + F4 关闭表单?
[Edit]
If you want to trap Alt-F4 then there's no point trying at the control level as that keystroke is handled by the application - see How to Disable Alt + F4 closing form?
您可以使用应用程序消息过滤器:
You can use an application message filter:
尝试创建一个观察者来捕获您的事件:
http://ondotnet .com/pub/a/dotnet/2002/04/15/events.html
Try creating an observer to capture your events:
http://ondotnet.com/pub/a/dotnet/2002/04/15/events.html