簡體   English   中英

C#+ USB條形碼閱讀器

[英]C# + USB Barcode Reader

如何在C#應用程序中閱讀背景中的條形碼文本 我用谷歌搜索但沒有用。 stackoverflow上的其他資源並不接近我所需要的。 我想在后台閱讀條形碼。 我想知道數據是來自條形碼還是鍵盤。 如果數據來自條形碼,則即使文本框突出顯示,也不得在文本框中顯示。 我有類似的stackoverflow代碼,但如果窗口中有文本框,則文本框將包含條形碼數據; 我不想要的。 鏈接: 獲取條形碼閱讀器值表單后台監控

    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();
        }
    }

大多數條形碼掃描儀只是作為鍵盤輸入,快速/簡單的解決方法是將文本框置於“視線外”。 一個例子是這樣的:

// 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等。

像鍵盤一樣的條碼設備。

1輸入條形碼時,按f12

2插入條形碼后按下了輸入

private void textbox_Keydown(object sender, KeyEventArgs e)
{
   if(e.KeyCode == Keys.F12){
    Textbox.Focus();
   }
   if(e.KeyCode == Keys.Enter){
     /// after enter barcode
     /// save 
   }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM