#630 – PreviewTextInput and TextInput Events

In addition to the four main keypress events–PreviewKeyDown, KeyDownPreviewKeyUp and KeyUp–an UIElement can fire two other events related to keyboard input.  Both fire when the user presses a key, or combination of keys, that results in the control receiving some text.  They do not fire when keys are pressed that don’t result in the keyboard sending text (e.g. the Backspace key).

Here’s the updated sequence of events:

  • PreviewKeyDown – tunneling
  • KeyDown – bubbling
  • PreviewTextInput  – tunneling
  • TextInput – bubbling
  • PreviewKeyUp – tunneling
  • KeyUp – bubbling

Note that controls that normally accept text and do something with it will suppress the TextInput event, marking it as handled.  For example, the TextBox control takes the text input and adds it to to the TextBox, so it marks TextInput as already handled.  The TextBox is saying that it already “handled” the text, so it doesn’t need to pass the event on to anybody else.

Advertisements