4
votes

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 key 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.

1

1 Answers

6
votes

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.