Why does PreviewTextInput not handle spaces? Why does PreviewTextInput not handle spaces? wpf wpf

Why does PreviewTextInput not handle spaces?


I found some sort of explanation in this WPF forum question:

Because some IMEs will treat whitespace keystroke as part of the text composition process, that's why it eats up by Avalon to report correct composited text through TextInput event.

And some more info from the MSDN documentation of the TextInput event:

... For keyboard input, WPF first sends the appropriate KeyDown/KeyUp events. If those events are not handled and the key is textual (rather than a control key such as directional arrows or function keys), then a TextInput event is raised. There is not always a simple one-to-one mapping between KeyDown/KeyUp and TextInput events because multiple keystrokes can generate a single character of text input and single keystrokes can generate multi-character strings. This is especially true for languages such as Chinese, Japanese, and Korean which use Input Method Editors (IMEs) to generate the thousands of possible characters in their corresponding alphabets.

When WPF sends a KeyUp/KeyDown event, Key is set to Key.System if the keystrokes could become part of a TextInput event (if ALT+S is pressed, for example). This allows code in a KeyDown event handler to check for Key.System and, if found, leave processing for the handler of the subsequently raised TextInput event. In these cases, the various properties of the TextCompositionEventArgs argument can be used to determine the original keystrokes. Similarly, if an IME is active, Key has the value of Key.ImeProcessed, and ImeProcessedKey gives the original keystroke or keystrokes.

BTW, Here are two related questions:


One workaround I have found is to hook PreviewKeyDown instead of PreviewTextInput. Unfortunately, that approach requires a lot more runaround to get a concrete char for what button they pressed. The most reliable way I found was in this question. It feels quite cumbersome for what I feel should be rather simple. Anyone have a better way of doing this?