从没有文本框的条形码阅读器获取数据 [英] Get data from barcode reader without textbox

查看:46
本文介绍了从没有文本框的条形码阅读器获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My barcode reader is a HID type and use the USB.

当文本框成为焦点时,我可以获取数据并执行一些业务逻辑.

I can get the data when the text box is focus and do some business logic.

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

我在Google上搜索后,发现了这条文章,并尝试实施到我的应用程序.但是现在的问题是,值返回为双字符.如果字符串为F1234,则将返回FF11223344,依此类推.

After I google, I found this article and try to implement to my application. But the problem now, the value return with double character. If string is F1234, it will return FF11223344 and so on.

此处是代码

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

需要建议来解决我的问题

Need advice to solve my problem

推荐答案

只需注释以下行

//this.KeyPress + =新的System.Windows.Forms.KeyPressEventHandler(this.Form1_KeyPress);

//this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.Form1_KeyPress);

这篇关于从没有文本框的条形码阅读器获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆