#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.
Thanks for the article Sean.
Previously I really wondered why the input bindings(PreviewKeyDown and PreviewKeyUp) are not working for Grid Control .now this article helps me 🙂
Thanks for the details!
useful article……
Thanks for the article. Very useful !
thanks again
oh my God…thank you.
I’ve had the same reaction myself, when finding an answer somewhere or figuring something out.
is it possible to copy this behavior to WinForms?
Not verbatim. These are WPF examples.