WPF KeyDown 和 Keyup 事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1871373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:18:20  来源:igfitidea点击:

WPF KeyDown and Keyup events

wpfeventskeydown

提问by TheWommies

Hi when I do in one of my user controls in a WPF application,

嗨,当我在 WPF 应用程序中的一个用户控件中进行操作时,

this.KeyUp += new KeyEventHandler(ControlViewer_KeyUp);

or

或者

this.KeyDown += new KeyEventHandler(ControlViewer_KeyUp);

or

或者

this.AddHandler(Window.KeyDownEvent, new KeyEventHandler(ControlViewer_KeyUp), true);

I can never get the key events to fire when pusing a key on the keyboard.

按下键盘上的键时,我永远无法触发按键事件。

Would anyone know why?

有谁知道为什么?

回答by Aran Mulholland

the event is probably being handled before you can get to it, if any handlers mark the KeyEventArgs e.Handled = true;you wont get the notification, try using the PreviewKeyDownor PreviewKeyUpevents and see if you have more luck.

该事件可能在您到达之前就已被处理,如果任何处理程序标记KeyEventArgs e.Handled = true;您将不会收到通知,请尝试使用PreviewKeyDownPreviewKeyUp事件,看看您是否有更多的运气。