#629 – Some Controls May Swallow Keypress Events
August 21, 2012 2 Comments
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.
Pingback: Dew Drop – August 22, 2012 (#1,386) | Alvin Ashcraft's Morning Dew
Pingback: #637 – Block Repeating Keys by Setting Handled to true « 2,000 Things You Should Know About WPF