区分扫描仪和键盘

时间:2011-04-12 05:57:18

标签: c# .net vb.net barcode

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

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

2 个答案:

答案 0 :(得分:2)

查看更新,截至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
        }
    }
}

答案 1 :(得分:0)

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