[英]How can I listen to keyboard events in all controls of a Window?
I have to listen to keyboard event in my Window, but if I use this code: 我必须在Window中监听键盘事件,但是如果使用以下代码:
MyWindow.KeyDown += new KeyEventHandler(MainWindow_KeyDown);
I don't get events fired inside other controls, like textbox. 我没有在其他控件(例如文本框)内触发事件。 How can I get events from ALL controls and stop the propagation of the event to its original control? 如何从ALL控件获取事件并停止将事件传播到其原始控件?
EDIT 编辑
I tried this code: 我尝试了这段代码:
EventManager.RegisterClassHandler(typeof(Window), Window.KeyDownEvent, new KeyEventHandler(MainWindow_KeyDown));
I works for most case, but for example it did not get the HOME key when I'm inside a textbox. 我在大多数情况下都可以工作,但是例如当我在文本框中时,它没有获得HOME键。
Have look into PreviewKeyDown : 看一下PreviewKeyDown :
this.PreviewKeyDown += MainWindow_PreviewKeyDown;
Event handler: 事件处理程序:
private void MainWindow_PreviewKeyDown(object sender, KeyEventArgs e)
{
// do some stuff
e.Handled = true // Stops propagating the event
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.