Convert received keys in PreviewKeyDown to string
I am using the PreviewKeyDown event on the window to get all the keys from the barcode scanner. KeyEventArgs is an enum and doesn't give me the actual string. I do not want to use TextInput as some of the keys can be handled by the control itself and cannot appear on the TextInput event.
I'm looking for a way to convert the keys I get in the PreviewKeyDown to an actual string. I looked at InputManager, TextCompositionManager, etc. but I don't find a way that I provide a list of keys and it returns with a string. TextCompositionManager or something should convert these Keys to a string that is available in TextInput.
source to share
Here is the event I'm using. KeyDown gets keys and PreviewTextInput gets actual text. So somewhere in between the keys are converted to text.
public Window1()
{
InitializeComponent();
TextCompositionManager.AddPreviewTextInputStartHandler(this, new TextCompositionEventHandler(Window_PreviewTextInput));
this.AddHandler(Window.KeyDownEvent, new System.Windows.Input.KeyEventHandler(Window_KeyDown), true);
}
private void Window_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
}
private void Window_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
}
source to share
Key -> Converting text is much harder than you think, there is really no way to map one key stroke to one character because in some languages ββand in some cases you need multiple keystrokes to create one character.
Since you are interested in input with a barcode scanner (which I believe will only generate a small subset of what Windows can handle, perhaps only ASCII, perhaps even less), you can build a conversion table yourself and write it to your program - it's much easier to deal with all the madness that Windows word processing does (for fun, finding "dead keys").
source to share