#629 – Some Controls May Swallow Keypress Events

If you are handling KeyUp and/or KeyDown events for a control, you’ll notice that certain keys may be “swallowed” by a particular control.  In other words, you might not get all of the keypress events.

A control may intercept a particular event and mark it as handled if it interprets the keypress and uses it within the control.

For example, a TextBox control will “swallow” the KeyDown event for the Backspace key, since this key is used to delete characters in the control.

In the example below, I press the ‘a’ key and see all four (PreviewKeyDown, KeyDown, PreviewKeyUp, KeyUp) keypress events.  However, when I press Backspace, I see only three events–the TextBox intercepts the KeyDown event, erases a character, and marks the event as handled.

 

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

2 Responses to #629 – Some Controls May Swallow Keypress Events

  1. Pingback: Dew Drop – August 22, 2012 (#1,386) | Alvin Ashcraft's Morning Dew

  2. Pingback: #637 – Block Repeating Keys by Setting Handled to true « 2,000 Things You Should Know About WPF

Leave a comment