获取数据从条码阅读器没有文本框

本文关键字:文本 条码阅读器 数据 获取 | 更新日期: 2023-09-27 18:18:28

我的条形码阅读器是HID类型,使用USB。

当文本框是焦点时,我可以获得数据并做一些业务逻辑。

 private void Form1_KeyUp(object sender, KeyEventArgs e)
 {
     if (e.KeyValue == (char)Keys.Return)
     {
           e.Handled = true;
           int barcodeLength = textBox1.TextLength;
           textBox1.Select(0, barcodeLength);
           queryData(textBox1.Text);    
     }
 }

我谷歌后,我找到了这篇文章,并试图实现到我的应用程序。但是现在的问题是,返回值带有双字符。如果string是F1234,它将返回FF11223344,以此类推。

这里的代码

    DateTime _lastKeystroke = new DateTime(0);
    List<char> _barcode = new List<char>(10);
     public Form1()
     {
          InitializeComponent();
          this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.Form1_KeyPress);
     }
     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());
             queryData(msg);
             _barcode.Clear();
         }
      }

需要建议来解决我的问题

获取数据从条码阅读器没有文本框

请注释下面一行

//。按键+= new System.Windows.Forms.KeyPressEventHandler(this.Form1_KeyPress);