有一种方法可以区分键盘和 USB 条码阅读器
从以下假设开始:
- 条码阅读器扫描的代码长度至少为 4 个字符
- 条码阅读器扫描的代码以“ENTER”键结束
- 传输整个条码不到 50 毫秒
这个使用 VS2005 VB 的简单表格包含:
- 文本框1
- 文本框2
- 文本框3
- 按钮1
- Timer1“时间间隔设置为50ms”
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