2009-03-05 151 views
11

有沒有辦法從USB條形碼讀取器讀取而忽略鍵盤並且不知道PID或VID USB掃描儀?我知道有一種方法可以通過使用USB掃描儀的VID和/或PID來區分USB掃描儀輸入和鍵盤輸入;這是使用http://nicholas.piasecki.name/blog/2009/02/distinguishing-barcode-scanners-from-the-keyboard-in-winforms/ 代碼完成的。但是,如果不將掃描儀的VID或PID放入配置文件(或源代碼)中,是否還有另一種解決方案來區分鍵盤和USB掃描儀?不希望將各種VID或PID放入配置文件的原因是,正在開發的應用程序將部署在衆多筆記本電腦上,並且隨機附帶有任意類型的掃描儀。使用USB條形碼掃描器讀取條形碼以及忽略鍵盤數據輸入,而掃描儀產品ID和供應商ID未知

此外,我不想配置掃描儀的輸出開始和結束順序,因爲掃描儀正在被同一臺機器上的其他軟件使用,我也不想更改其他軟件上的代碼。我不希望將條形碼閱讀器編程爲串行模式,原因可能與之前提到的相同。

回答

13

有鍵盤和USB條形碼閱讀器來區分的方式

你可以依靠這些事實:

  1. 通過條碼閱讀器在minmum 4個字符掃描碼
  2. 通過掃描QR碼條碼閱讀器以RETURN「ENTER」結尾
  3. 掃描孔條碼需要不到50毫秒的時間

這是使用VS2005 VB一個簡單的形式包含:

  1. TextBox1中
  2. TextBox2中
  3. textbox3
  4. Button1的
  5. 定時器1 「的時間間隔設置爲50」 MS」

Public Class Form1 

Dim BarcodeStr As String = "" 
Dim IsBarcodeTaken As Boolean = False 
Dim Str As String = "" 
Dim str3 As String = "" 


Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown 

    If Timer1.Enabled = False Then 
     Str = TextBox1.Text 
     str3 = TextBox3.Text 
    End If 

End Sub 

Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress 
    If Timer1.Enabled = False Then 
     Timer1.Enabled = True 
    End If 


    BarcodeStr = BarcodeStr & e.KeyChar 
    If Asc(e.KeyChar) = 13 And Len(BarcodeStr) >= 4 Then 
     IsBarcodeTaken = True 
     TextBox2.Text = BarcodeStr 


    End If 

End Sub 
Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp 
    If IsBarcodeTaken = True Then 
     TextBox1.Text = Str 
     TextBox1.Select(Len(TextBox1.Text), 0) 
     Str = "" 

     TextBox3.Text = str3 
     TextBox3.Select(Len(TextBox3.Text), 0) 
     str3 = "" 
    End If 

End Sub 


Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick 
    BarcodeStr = "" 
    IsBarcodeTaken = False 
    Timer1.Enabled = False 
End Sub 


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
    TextBox2.Text = "" 

End Sub 

End Class 
+0

由於我沒有足夠的代表來編輯其他人的帖子,請讓我爲你翻譯:「假設掃描儀至少輸入4個字符,以ENTER鍵結束,並採用50ms以下的操作。」這聽起來對我來說是個好主意,但也許你應該讓時間爲100ms。 – MiffTheFox 2009-06-18 12:03:16

+0

恐怕,這是不正確的。您可以**通過使用[原始輸入](https://msdn.microsoft.com/en-us/library/windows/desktop/ms645536.aspx)告訴哪個設備生成輸入。您不需要知道VID和PID,因爲USB掃描儀具有指定的* UsagePage *和* Usage *條目。 – IInspectable 2017-01-09 16:21:00

0

還有一個關於條碼here的問題,該鏈接會通過串口向您發送一個使用條形碼的答案。也許這對你來說是一個解決方案?

恕我直言:最簡單的解決方案將接受來自鍵盤的輸入。

+0

此外,我不想將掃描器配置爲串行模式,因爲掃描儀正在被同一臺機器上的其他軟件使用,我不希望更改其他軟件上的代碼。 – 2009-03-05 14:52:09

0

也許這是一個過於簡單的解決方案,但是你能捕捉到按鍵事件並簡單地通過鍵盤禁止輸入嗎?

3

那麼,我正在使用一個很像Ehab的解決方案 - 我只是爲我的應用程序清理了一些代碼。我正在使用我的編輯控件的自定義類(它也在做其他一些操作) - 但這些是這個的重要部分:#

public class ScannerTextBox : TextBox 
    { 
     public bool BarcodeOnly { get; set; } 

     Timer timer; 

     private void InitializeComponent() 
     { 
      this.SuspendLayout(); 

      this.ResumeLayout(false); 
     } 

     void timer_Tick(object sender, EventArgs e) 
     { 
      if (BarcodeOnly == true) 
      { 
       Text = ""; 
      } 

      timer.Enabled = false; 
     } 


     protected override void OnKeyPress(KeyPressEventArgs e) 
     { 
      base.OnKeyPress(e); 

      if (BarcodeOnly == true) 
      { 
       if (timer == null) 
       { 
        timer = new Timer(); 
        timer.Interval = 200; 
        timer.Tick += new EventHandler(timer_Tick); 
        timer.Enabled = false; 
       } 
       timer.Enabled = true; 
      } 

      if (e.KeyChar == '\r') 
      { 
       if (BarcodeOnly == true && timer != null) 
       { 
        timer.Enabled = false; 
       } 
      } 
     } 
    }