2

I have a computer with a USB keyboard and a USB Barcode Scanner that works sending the readed codes as very fast keystrokes.

Now my app it's a 100% keylogger but I want to differentiate the usb Keyboard with the Barcode Scanner to send only Barcodes to the keylogger.

This is the code I'm using.

Declarations

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

Keyboard Hook

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

Keyboard callback method

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

Key translation

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

Log

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
freaker
  • 21
  • 7
  • Might find this question useful: https://stackoverflow.com/questions/17397175/vb6-reading-from-keyboard-in-an-activex-dll – wqw Oct 23 '17 at 19:34

1 Answers1

0

I've done similar with barcode input which is terminated with a carriage return.

  1. Start by making a global variable which will be used to track the value in Timer (seconds elapsed since midnight). For the same of this example, we'll call it StartOfInput.
  2. Set StartOfInput to 0.
  3. When you receive a character in your text input, check to see if StartOfInput is zero. If it is, save the current Timer value to StartOfInput.
  4. No subsequent character input changes that value, since it's only changed if StartOfInput is zero.
  5. When you get a carriage return (or whatever else your postable is for the barcode scanner) you would compare the difference in the current value in Timer to see if it's less than StartOfInput by a certain threshold. For instance, should your user be able to type 30 characters in 1 second? Unlikely, but a barcode scanner can.
Luke G.
  • 587
  • 1
  • 4
  • 13