Capture Keyboard Input in WPF

In case you ever need to capture some keyboard input similar to a TextBox but without a TextBox (hence you also want to capture input that - on some keyboard layouts - need a secondary key to type (like % on my layout = Shift+5), but don't care whether the user presses special keys like Shift or Ctrl, or whether the num block was used to enter a number): Don't try to start with KeyDown or KeyUp, they won't help much. Instead, register a text input handler on the TextCompositonManager:

1: 
2: 
TextCompositionManager.AddTextInputHandler(this,
    new TextCompositionEventHandler(OnTextComposition));

...and then simply use the event handler argument's Text property:

1: 
2: 
3: 
4: 
5: 
private void OnTextComposition(object sender, TextCompositionEventArgs e)
{
    string key = e.Text;
    ...
}

And you get the keyboard layout and system key tracking for free.