views:

258

answers:

4

I am using PreviewKeyDown event on a window to receive all the keys from a barcode scanner. The KeyEventArgs is an enumeration and does not given me the actual string. I dont want to use TextInput as some of the keys may get handled by the control itself and may not bubble up to the TextInput event.

I am looking for a way to convert the the Keys that I get in PreviewKeyDown to actual string. I looked at the InputManager, TextCompositionManager etc but I am not finding a way where I give the list of keys and it comes back with a string. TextCompositionManager or something must be converting these Keys to a string which is what is available in TextInput.

A: 

Ok, stolen from this post.

DllImport("user32.dll")]
static extern short VkKeyScan(char ch);

static public Key ResolveKey(char charToResolve)
{    
    return KeyInterop.KeyFromVirtualKey(VkKeyScan(charToResolve));
}
Gregory
e as PreviewKeyDownEventArgs gives a compile error. KeyEventArgs cannot be cast to PreviewKeyDownEventArgs
Ragha J
I want to help you here, but you need to give me more info. Can you add the event definition, or perhaps a link via MSDN to the question?
Gregory
I couldnt post the entire thing in comments. So I posted it in the answer below.
Ragha J
Edited re. your comments/extra post.
Gregory
A: 

Here is the event that I am using. The KeyDown gets the keys and the PreviewTextInput gets the actual text. So somewhere in between the keys are getting 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)
            {
            }
Ragha J
A: 

Key -> Text conversion is much more complicated than you think, there is actually no way to map a single key stroke to a single character because in some languages and some cases you need multiple keystrokes to compose a single character.

Since you are interested in input from a barcode scanner (that I assume will only generate a small subset of what windows can handle, maybe only ASCII maybe even less) you can build the conversion table yourself and hard code it into your program - it's much easier then to handle all the craziness that Windows text handling does (for fun, lookup "dead keys").

Nir
A: 

Ragha, your workaround is great! Since I cannot add comments or vote yet (lack of points) I am posting this as answer.

That the .NET framework returns the raw scan codes on KeyUp/KeyDown events is simply "impossible" (as in "too shocking to be true"). So what do we do? Use the PreviewTextInput event instead. We get the string representation of the key pressed like this:

void KeyboardNavigation(object sender, TextCompositionEventArgs e)
{
   string typedChar = e.Text;
}
Helge Klein