经过长时间的练习和咨询互联网,我已经能够区分 键盘输入已成功扫描输入。
我有2个文本框(txtScannedInput和txtKeyboardInput)和 1个标签(lblDisplayScannedInput)显示扫描输入。
当txtScannedInput文本框之前具有焦点时,一切都很完美 扫描条形码。
我唯一的问题是txtKeyboardInput之前有焦点 扫描条形码条形码的前2个字符保留在txtKeyboardInput中 而其余的字符移动到txtScannedInput。
(我知道这是因为我的计算是区分键盘输入 扫描输入基于按键,因此当它知道输入是扫描时 其中一个字符已经输入到txtKeyboardInput中,它将移动到txtScannedInput。)
任何人都可以帮助我改进我的代码,还是有更好的方法来做到这一点?
Public Class frmFinalBarCode
Dim datLastTimePress As DateTime = Date.Now
Private Sub frmFinalBarCode_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
If (DateTime.Now.Subtract(datLastTimePress).Milliseconds > 50) Then
Me.Text = "This is a keyboard input"
Else
Me.Text = "This is a Scanner input"
datLastTimePress = DateTime.Now ' Initialize the timer for the first character
txtScannedInput.Focus()
End If
End Sub
Private Sub frmFinalBarCode_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
datLastTimePress = Date.Now
End Sub
Private Sub txtScannedInput_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtScannedInput.KeyPress
lblDisplayScannedInput.ResetText()
lblDisplayScannedInput.Text = txtScannedInput.Text
End Sub
End Class