gpt4 book ai didi

C# + USB 条码阅读器

转载 作者:行者123 更新时间:2023-11-30 20:56:33 25 4
gpt4 key购买 nike

如何在我的 C# 应用中读取后台的条形码文本?我用谷歌搜索但没有用。 stackoverflow 上的其他资源并不接近我需要的资源。我想在后台阅读条形码。我想知道数据是来自条形码还是键盘。如果数据来自条形码,则即使文本框高亮显示,也不能显示在文本框上。我得到了类似的 stackoverflow 代码,但是如果窗口中存在文本框,那么文本框将包含条形码数据;我不想要。链接: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();
}
}

最佳答案

大多数条码扫描器只是充当键盘输入,一个快速/简单的解决方法是放置一个文本框“看不见”。一个例子是这样的:

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

关于C# + USB 条码阅读器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17475210/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com