#588 – If You Handle PreviewKeyDown Event, KeyDown Won’t Fire

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.


About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

9 Responses to #588 – If You Handle PreviewKeyDown Event, KeyDown Won’t Fire

  1. 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 🙂

  2. Hemant says:

    Thanks for the details!

  3. Balu says:

    useful article……

  4. Roman says:

    Thanks for the article. Very useful !

  5. jonbakari says:

    thanks again

  6. Tanya says:

    oh my God…thank you.

  7. b says:

    is it possible to copy this behavior to WinForms?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: