本文介绍了C#+ USB条码读取器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何在C#应用程序的背景下阅读条形码文本?我用谷歌搜索,但没有用.和其他关于stackoverflow的资源都不符合我的需求.我想在后台读取条形码.我想知道数据是否来自条形码或键盘.如果数据来自条形码,则即使文本框已突出显示,也不得将其显示在文本框上.我有类似的代码用于stackoverflow,但是如果窗口中存在文本框,则该文本框将包含条形码数据.我不想要的.链接:获取条形码阅读器值形式的后台监控

How can i read barcode text in background in my C# app? I googled but its of no use. And other resources on stackoverflow are not close to what i need. I want to read barcode in background. And i want to know if the data is coming from barcode or key board. If the data comes from the barcode then it mustnot be displayed on textbox even though the textbox is highlighted. I got similar code for stackoverflow but if there is presence of textbox in the window then the textbox will contain barcode data; which i dont want.Link : get barcode reader value form background monitoring

    DateTime _lastKeystroke = new DateTime(0);
    List<char> _barcode = new List<char>(10);

    private void Form1_KeyPress(object sender, KeyPressEventArgs e)
    {
        // check timing (keystrokes within 100 ms)
        TimeSpan elapsed = (DateTime.Now - _lastKeystroke);
        if (elapsed.TotalMilliseconds > 100)
            _barcode.Clear();

        // record keystroke & timestamp
        _barcode.Add(e.KeyChar);
        _lastKeystroke = DateTime.Now;

        // process barcode
        if (e.KeyChar == 13 && _barcode.Count > 0) {
            string msg = new String(_barcode.ToArray());
            MessageBox.Show(msg);
            _barcode.Clear();
        }
    }

推荐答案

大多数条形码扫描仪仅充当键盘输入,而快速/轻松的解决方法是将文本框置于视线之外".一个例子就是这样:

Most barcode scanners simply act as keyboard inputs and a quick / easy work around is to place a textbox "Out of sight". An example would be something like this:

// Pseudo code (could be Web, Windows etc)
public void Form1_Load()
{
    txtBarcodeScanner.Top = -10000;
    txtBarcodeScanner.Left = -10000;
    txtBarcodeScanner.Width = 10;
    txtBarcodeScanner.Height = 10;
    txtBarcodeScanner.Focus();
}

这样,输入可以被txtBarcodeScanner捕获,但是将不可见,并且条形码也不会被捕获,但是会触发KeyDown等.

That way the input can be captured by txtBarcodeScanner but will not be visible and the barcode will not seen being captured but will fire KeyDown etc.

这篇关于C#+ USB条码读取器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 11:04