WPF KeyDown和Keyup事件[重复]

时间:2021-11-26 00:08:39

This question already has an answer here:

这个问题在这里已有答案:

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?

谁会知道为什么?

1 个解决方案

#1


5  

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 PreviewKeyDown or PreviewKeyUp events and see if you have more luck.

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

#1


5  

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 PreviewKeyDown or PreviewKeyUp events and see if you have more luck.

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