Barcode scanner with a WPF application

你。 提交于 2020-01-05 04:28:14

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/38193313/barcode-scanner-with-a-wpf-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!