开发者

Ways to intercept key presses in a WPF app natively

开发者 https://www.devze.com 2023-02-26 03:00 出处:网络
What\'s the best method for intercepting key presses and handling them in a WPF app, avoiding the control with focus getting the keypress?

What's the best method for intercepting key presses and handling them in a WPF app, avoiding the control with focus getting the keypress?

I need the application to handle a sequence of key presses (including arrow keys) and avoid a control with focus (i.e. Treeview) getting the k开发者_开发知识库ey press events. I've seen ways of doing this with interop but was hoping there was support for this natively within a WPF application?

Thanks.


You can attach to the PreviewKeyDown event of the root element in your application, which is typically a Window. If you set e.Handled to true, then you will normally prevent anything else from getting the PreviewKeyDown or the KeyDown event.

In addition, you may need to handle the PreviewTextInput event, as this is what text boxes or other editors normally use.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号