VB6检查键入的键盘[BARCODE SCANNER]

时间:2017-10-23 08:46:04

标签: keyboard vb6 hook barcode reader

我的计算机配有USB键盘和USB条形码扫描仪,可以快速击键发送重新编码的代码。

现在我的应用程序是100%键盘记录程序,但我想区分usb键盘和条形码扫描仪,只将条形码发送到键盘记录器。

这是我正在使用的代码。

声明

Private Declare Function SetWindowsHookEx Lib "user32.dll" _
    Alias "SetWindowsHookExA" (ByVal idHook As Long, _
                                ByVal lpfn As Long, _
                                ByVal hmod As Long, _
                                ByVal dwThreadId As Long) As Long

Private Declare Function UnhookWindowsHookEx Lib "user32.dll" (ByVal hHook As Long) As Long

Private Declare Function CallNextHookEx Lib "user32.dll" (ByVal hHook As Long, _
                                                          ByVal nCode As Long, _
                                                          ByVal wParam As Long, _
                                                          ByRef lParam As Any) As Long

Private Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (ByRef Destination As Any, _
                                                                         ByRef Source As Any, _
                                                                         ByVal Length As Long)

Private Declare Function GetAsyncKeyState Lib "user32.dll" (ByVal vKey As Long) As Integer

Private Const WH_KEYBOARD_LL As Long = 13

Public Type KBDLLHOOKSTRUCT
    VkCode As Long
    ScanCode As Long
    Flags As Long
    Time As Long
    DwExtraInfo As Long
End Type

Dim KBHook As Long
Dim KeyData As String

Public Sub main()
    ManageKeylogger True
End Sub

键盘挂钩

Public Sub ManageKeylogger(ByVal Enable As Boolean)
    Select Case Enable
        Case True
            KBHook = SetWindowsHookEx(WH_KEYBOARD_LL, AddressOf KBProc, App.hInstance, 0)
        Case False
            Call UnhookWindowsHookEx(KBHook)
    End Select
End Sub

键盘回调方法

Public Function KBProc(ByVal nCode As Long, ByVal wParam As Long, lParam As Long) As Long
    Dim KeyBoardHook As KBDLLHOOKSTRUCT

    If nCode = 0 Then
        CopyMemory KeyBoardHook, lParam, Len(KeyBoardHook)
        With KeyBoardHook
            If .Flags = 0 Or .Flags = 1 Then
                If SaveLog(TranslateKey(.VkCode)) > 0 Then
                    Call LogToFile(App.Path & "\Log.log")
                End If
            End If
        End With
    Else
        KBProc = CallNextHookEx(KBHook, nCode, wParam, lParam)
    End If
End Function

关键翻译

Private Function TranslateKey(ByVal KeyCode As Long) As String
'stuff
End Function

日志

Public Function SaveLog(ByVal sKey As String) As Double
    KeyData = KeyData & sKey
    SaveLog = Len(KeyData)
End Function

Public Sub LogToFile(ByVal sPath As String)
    Open sPath For Binary As #1
        Put #1, , KeyData
    Close #1
End Sub

1 个答案:

答案 0 :(得分:0)

我做过类似的条形码输入,以回车符结束。

  1. 首先创建一个用于跟踪的全局变量 计时器中的值(自午夜起经过的秒数)。对于同样的 这个例子,我们称之为StartOfInput。
  2. 将StartOfInput设置为0。
  3. 当您在文本输入中收到字符时,请检查是否 StartOfInput为零。如果是,请将当前的Timer值保存到 StartOfInput。
  4. 没有后续字符输入更改该值, 因为只有在StartOfInput为零时它才会改变。
  5. 当您收到回车符(或条形码扫描仪的任何其他内容)时,您将比较当前值的差异。 定时器查看它是否小于StartOfInput一定的阈值。 例如,您的用户是否可以在1中键入30个字符 第二?不太可能,但条形码扫描仪可以。