#588 – If You Handle PreviewKeyDown Event, KeyDown Won’t Fire
June 26, 2012 9 Comments
The PreviewKeyDown and KeyDown events in WPF are paired routed events. When a user presses a key in a control, the PreviewKeyDown event fires first, as a tunneling event. When the event has propagated down the logical tree to the control where the key press originated, the KeyDown event fires. KeyDown propagates up the logical tree, since it is defined as a bubbling event.
If you handle the PreviewKeyDown event as it is propagating down the tree and you mark the event as handled (setting KeyEventArgs.Handled to true), the PreviewKeyDown event will not continue propagating down the tree.
But if you mark PreviewKeyDown as handled, the corresponding KeyDown event will not fire at all. This works because the two events share the same instance of a KeyEventArgs object, so when PreviewKeyDown marks the event as handled, KeyDown also treats the event as handled.