Search code examples
c#wpfkeyeventargs

Convert a System.Windows.Input.KeyEventArgs key to a char


I need to get the event args as a char, but when I try casting the Key enum I get completely different letters and symbols than what was passed in.

How do you properly convert the Key to a char?

This is what I've tried

ObserveKeyStroke(this, new ObervableKeyStrokeEvent((char)((KeyEventArgs)e.StagingItem.Input).Key));

Edit: I also don't have the KeyCode property on the args. I'm getting them from the InputManager.Current.PreNotifyInput event.


Solution

  • See How to convert a character in to equivalent System.Windows.Input.Key Enum value? Use KeyInterop.VirtualKeyFromKey instead.