2011-04-12 84 views
0

嘿所有。 我有一臺條形碼掃描儀連接到一臺使用C#程序的電腦。現在我想區分掃描儀和鍵盤哪一個正在向我的程序發送數據。 每個人都可以幫我一個代碼或建議在C#中?區分掃描儀和鍵盤

有人在另一個話題中對我說過這件事(但我還不能這樣做): 基本上,您可以配置掃描儀發送一些基本上可以告訴計算機的字符「嗨,這就是我」。當您在輸入流中看到這些字符時,您知道信息來自條形碼掃描器,而不是來自用戶在鍵盤上鍵入的內容。你檢查過你的條形碼掃描儀附帶的手冊嗎?它應該有更多關於這方面的信息。

+0

如果掃描儀是系統設備管理器中的HID鍵盤設備,則它是鍵盤。 – Enigmativity 2016-07-05 05:42:56

回答

2

如果您的應用程序使用特定的條形碼(例如,所有相同的字符長度或可以與RegEx匹配的字符),那麼您可以通過編寫一個Robot Typing測試來解決這個問題。例如:

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 
     } 
    } 
} 
0

掃描數據將典型地以回車或換行字符結尾,稱爲後綴。您也許可以配置掃描儀以包含前綴。這就是你的朋友試圖告訴你的。

+0

謝謝。但我做了不同的方式。我用這個鏈接來確定哪一個(鍵盤或條形碼掃描器發送輸入)http://www.codeproject.com/KB/system/rawinput.aspx – 2011-04-12 07:40:51