大家好。我有一个连接到PC的条形码扫描仪,它与c# program.now一起工作,我想区分扫描仪和键盘,哪个正在向我的程序发送数据。每个人都可以在c#中帮助我编写代码或提供建议吗?
有人在另一个主题中对我说了这句话(但我还不能这么做):基本上你可以配置扫描器发送一些字符,基本上告诉计算机“嗨,是我”。当您在输入流中看到这些字符时,您就知道信息来自条形码扫描仪,而不是来自用户在键盘上键入的内容。你看过条形码扫描仪附带的说明书了吗?它应该有更多关于这方面的信息。
发布于 2011-04-12 14:19:26
请参阅更新,截至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 SubC#:
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
}
}
}发布于 2011-04-12 14:57:43
扫描数据通常以回车符或换行符结束,称为后缀。您也可以将扫描仪配置为包含前缀。这就是你的朋友想告诉你的。
https://stackoverflow.com/questions/5630962
复制相似问题