【问题标题】:distinguish between the scanner and the keyboard区分扫描仪和键盘
【发布时间】:2011-08-03 14:38:34
【问题描述】:

大家好。 我有一个连接到使用 c# 程序的 PC 的条形码扫描仪。现在我想区分扫描仪和键盘,哪个正在向我的程序发送数据。 每个人都可以帮我写代码或用 c# 提供建议吗?

有人在另一个话题中对我说这个(但我还不能这样做): 基本上你可以配置扫描仪发送一些基本上告诉计算机“嗨,是我”的字符。当您在输入流中看到这些字符时,您就知道这些信息来自条形码扫描仪,而不是来自用户在键盘上键入的内容。您是否查看了条形码扫描仪随附的手册?它应该有更多关于这方面的信息。

【问题讨论】:

  • 如果扫描仪是系统设备管理器中的 HID 键盘设备,那么它就是键盘。

标签: c# .net vb.net barcode


【解决方案1】:

查看截至 2019 年 3 月的更新: https://stackoverflow.com/a/55411255/495455


如果您的应用程序使用特定的条形码(例如所有相同的字符长度或可以与 RegEx 匹配的条形码),那么您可以通过编写机器人打字测试来解决它。例如:

VB.Net:

Private sw As Stopwatch
Private Sub FirstCharacterEntered()
    sw.Start()
End Sub
Private Sub txt_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt.TextChanged
    If txt.length = 0 Then FirstCharacterEntered()
    If txt.Length = BarCodeSerialLength Or New RegularExpressions.Regex("your pattern").IsMatch(txt.Text) Then
        sw.Stop()
        If sw.ElapsedMilliseconds < TimeAHumanWouldTakeToType Then
            'Input is from the BarCode Scanner
        End If    
    End If
End Sub

C#:

private Stopwatch sw;
private void FirstCharacterEntered()
{
    sw.Start();
}
private void txt_TextChanged(System.Object sender, System.EventArgs e)
{
    if (txt.length == 0)
        FirstCharacterEntered();
    if (txt.Length == BarCodeSerialLength | new RegularExpressions.Regex("your pattern").IsMatch(txt.Text)) {
        sw.Stop();
        if (sw.ElapsedMilliseconds < TimeAHumanWouldTakeToType) {
            //Input is from the BarCode Scanner
        }
    }
}

【讨论】:

    【解决方案2】:

    扫描数据通常以回车符或换行符结束,称为后缀。您也可以将扫描仪配置为包含前缀。这就是你的朋友想要告诉你的。

    【讨论】:

    猜你喜欢
    • 2011-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-19
    • 1970-01-01
    • 2019-01-08
    相关资源
    最近更新 更多