distinguish between the scanner and the keyboard

南楼画角 提交于 2019-12-20 06:28:23

问题


hey all. I have a barcode scanner connected to a PC that working with a c# program.now i want to distinguish between the scanner and the keyboard which one is sending data to my program. can everyone help me with a code or advise in c#?

somebody said this to me in another topic(but i can't do this yet): basically you can configure the scanner to send some characters that basically tell the computer "hi, it's me". When you see those characters in your input stream, you know the information is coming from the barcode scanner, not from something the user typed on the keyboard. Did you check the manual that came with your barcode scanner? It should have more information about this.


回答1:


See update, as at March 2019: https://stackoverflow.com/a/55411255/495455


If your application works with particular barcodes (eg all same char length or ones that can be matched with a RegEx) then you could possibly work it out by coding up a Robot Typing test. eg:

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:


The scan data will typically end with a carriage return or line feed character, called the suffix. You may be able to configure the scanner to include a prefix as well. That is what your friend was trying to tell you.



来源:https://stackoverflow.com/questions/5630962/distinguish-between-the-scanner-and-the-keyboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!