4

I have a barcode scanner (bluetooth) connected to my computer to use for scanning some barcodes. The scanner acts exactly like a keyboard does and returns whatever it scans. In my WPF application I have some textboxes for the user to manually enter in a product number, revision number, bin number, and lot number.

I would like the user to be able to instead scan the QR/Bar? code which has all that information in it at any time. So I have a few issues:

  1. Is it possible to scan a barcode with your focus on anything in the app and NOT write it out like a keyboard? Example, I have a random textbox highlighted right now but I go and scan a code - I dont want the code to fill in this random textbox - I would want something like all the scanned text goes into a variable.

  2. If step 1 is not possible. The way I currently have it set up is you need to click into a specific textbox. Which on TextChanged will parse it and try to figure out where the pieces need to go. But it will trigger when each character is added to the box. I have about ~30 characters per code so this will slow it down tremendously. I tried to see what other event might work but I don't see any other solution to that issue? Is there some other event that I'm just missing?

Thank you

Fivestar
  • 323
  • 1
  • 5
  • 18
  • The most elegant solution would be if the scanner didn't act as a keyboard and you could access it as a separate device. But barring that, what exactly did you do in your TextChanged event handler? If it's just simple string parsing it should not slow any device down "tremendously", or in fact even be noticeable. – Matti Virkkunen Jul 04 '16 at 23:30
  • If your scanner is connected via Bluetooth, you can also assign a SerialPort to that Bluetooth. It means, you can access your scanner through this virtual SerialPort. Then open the SerialPort and listen to event DataReceived. If you received the data, you can put the data anywhere you want and not depends in control focus. –  Jul 04 '16 at 23:34
  • @MattiVirkkunen In the TextChanged handler I take the text in the box and do something like. The first 7 characters go into this textbox. The next 10 throw out (they are spaces). Then the next 3 go into this box. The next 2 go into this box, etc. The issue with it is the scanner will act as a keyboard - so every time it enters a single letter it will go through the TextChanged event which freezes the program for a few seconds – Fivestar Jul 05 '16 at 02:34
  • @x... Okay that sounds much better. I have worked with serial ports once and used a data received handler. Tomorrow I will try to see if I can find info on that if I can't get Jeremy Thompsons answer working. Thank you! – Fivestar Jul 05 '16 at 02:43
  • I still refuse to believe simple string processing freezes a modern device for a few seconds. @x...: Can you really access a Bluetooth HID device as a serial port...? – Matti Virkkunen Jul 05 '16 at 08:44
  • Yes, I wrote that code about 10 years ago using Bluetooth with virtual serial port. Then you can access that scanner like a serial port scanner. Just wait for DataReceived. Yellow hand scanner, but I forgot which manufacturer. –  Jul 05 '16 at 08:51

1 Answers1

4

I dont want the barcode to fill random textboxes - I want all the scanned QR/barcode into a variable.

private string barCode = string.Empty; //TO DO use a StringBuilder instead
private void Window_Loaded(System.Object sender, System.Windows.RoutedEventArgs e)
{
    MainWindow.PreviewKeyDown += labelBarCode_PreviewKeyDown;
}

private void labelBarCode_PreviewKeyDown(object sender, KeyEventArgs e)
{
    if ((44 == e.Key)) e.Handled = true;
    barCode += e.Key;
    //look for a terminator char (different barcode scanners output different 
    //control characters like tab and line feeds), a barcode char length and other criteria 
    //like human typing speed &/or a lookup to confirm the scanned input is a barcode, eg.
    if (barCode.Length == 7) {
       var foundItem = DoLookUp(barCode);
       barCode = string.Empty;
    }
}

See update, as at March 2019: https://stackoverflow.com/a/55411255/495455

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • 1
    Also recommended to put a timer so that the value is resetted after some time - this will allow user to re-scan the barcode. – Jai Jul 05 '16 at 01:18
  • I'll try to give this a go tomorrow. The barcode does not have any terminator character, it could be set up but it is for an inventory system that is already in place and has been for a long time - I'm in no position to make changes to it. They do all inventory count by paper and need something to do it faster. Also could you explain what the if (44 == e.Key) is doing? and then the barCode += e.Key? – Fivestar Jul 05 '16 at 02:39
  • 1
    The 44 is just looking for the Delete key and ignoring it. The `barCode += e.Key` is concatenating the scanned input, 1, 1+0, 10+1, 101+1 = 1011. If you dont have a terminator character you will have to use the other criteria I suggested and possibly Jai's timer idea. Here is a basic implementation to check if the *speed* **text typed/inputted in** is faster than a human: http://stackoverflow.com/a/5631146/495455 – Jeremy Thompson Jul 05 '16 at 02:44
  • @JeremyThompson Okay so I did the timer idea - thank you for editing your other answer to include a C# version! Very helpful. If this method causes problems in the future I might try the virtual serial port idea. But thank you! – Fivestar Jul 05 '16 at 21:18