0

I am working with a scanner who is sending me a Barcode like this: "¬00111111111112222222" as pressed Keys like an USB Keyboard. What I have to do is to set the Focus on a TextBox whenever I get the prefix "¬".

The Problem right now is that inside my KeyDown Event the KeyEventArgs is "System" not the character from my prefix. Is there a way to compare this sign to set the Focus?

The Code I tried is:

private void KeyDownEvent(object sender, KeyEventArgs e)
{
    if (Convert.ToString(e.Key) == "¬")
    {
        myTextBox.Focus();
    }
}

Update: I tried e.SystemKey right now and at this point, I get LeftAlt as Event argument. So maybe the AISCII-code is converted to this SystemKey LeftAlt?

Update: I logged all the keys I get when I scan a Barcode and I noticed that I do not get the ASCII-Code itself as I thought. What I get is the Key Combination to make this Symbol like ”Alt+NumPad0+NumPad7+NumPad2” so now I only have to match them with the ASCII-Code from the "¬" Symbol and then it have to work. Thanks for all the help..

Teroman
  • 13
  • 4
  • What about `KeyInterop.VirtualKeyFromKey(e.Key);` ? – Majestic Jun 21 '16 at 09:49
  • Are you sure that your scanner is returning exactly the KeyDown event? Normally the KeyEventArgs e should contain the details of the pressed key. How can the KeyDown event fire without any trigger? Is there any way you can get the whole string or something? – ViVi Jun 21 '16 at 10:11
  • [This](http://stackoverflow.com/questions/5825820/how-to-capture-the-character-on-different-locale-keyboards-in-wpf-c) Might help you. But you might do some adjustments – lokusking Jun 21 '16 at 10:23
  • See http://stackoverflow.com/questions/5825820/how-to-capture-the-character-on-different-locale-keyboards-in-wpf-c – ardila Jun 21 '16 at 10:29
  • I tried your answer but the virtual key I get is 144 that stands for NumLock and there is no difference at this point to the real NumLock Button on the Key Board. So that dosent work. – Teroman Jun 22 '16 at 08:56
  • @Teroman whose answer? Use the @ sign before the user name to tag the person you're answering to. – ardila Jun 23 '16 at 07:53

1 Answers1

0

Try reading the serial input buffer (serial comm sample) and parse the string to check for special characters. check here: http://www.codeproject.com/Articles/678025/Serial-Comms-in-Csharp-for-Beginners

OR You can just take the ascii values and compare to a particular value or range. For eg., less than 65 or greater than 126.

  • I am not sure but I think your first advice only works with a Comport, right? However, I only got USB. I tried the second one, but the problem seems to be the KeyEventArgs is changing the whole char to LeftAlt. One more thing I realised is that if I write the text into a text Box I am able to get the right character from TextBox.Text so it have to give a temporary save of the real pressed key. Maybe I could read what is inside of this Cach. – Teroman Jun 21 '16 at 12:55
  • I have tried the 1st option with rs232 rs422 and usb. Works brilliantly everywhere. However, you can try whatever you feel is promising. – Dragon Fury Jun 22 '16 at 10:37
  • the big Problem in this solution is that I am working with WPF and not Windows Forms. I sadly do not have the KeyChar property on my KeyEventArgs. I also got no other property which show me the ASCII code of the KeyEventArgs. – Teroman Jun 22 '16 at 12:18