#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.

 

Advertisements