-
Oct 23rd, 2017, 09:30 AM
#1
Thread Starter
New Member
Check which keyboard is typing [BARCODE SCANNER]
Hi!
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 the readed Barcodes to the keylogger. And if it's possible catch the barcode reader keystrokes to not be written in the focused window.
This is the code I'm using.
Declarations
Code:
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
Code:
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
Code:
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
Code:
Private Function TranslateKey(ByVal KeyCode As Long) As String
'Stuff
End Function
Log
Code:
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
-
Oct 23rd, 2017, 09:38 AM
#2
Re: Check which keyboard is typing [BARCODE SCANNER]
I think you may be out of luck here. At least not with out some really low level stuff going on... By the time anything is going to see it, including the API hooks, the keys are already in the keyboard buffer, so there's no differentiation on their source. If you are looking to intercept the data from the scanner, then I think you're going to need to get down and dirty and talk to the USB directly, and find the right port and read directly from it, preventing it from getting into the keyboard buffer in the first place. That's defiantly at a lower level than where you're at now. It's also not something I know how to do, but I know there are others around here who are more knowledgable about this sort of thing than I am, maybe they can shed some light on it.
-tg
-
Oct 23rd, 2017, 09:42 AM
#3
Re: Check which keyboard is typing [BARCODE SCANNER]
Yeah I think if you want to do anything more with a scanner other than just see the input as keystrokes then you need to use a different interface. For example if you use one that uses an RS232 interface then you can detect what the scanner sends, what the code type is and possibly other information without much issue but when you are using a simple keyboard wedge interface your hands are tied.
-
Oct 23rd, 2017, 10:01 AM
#4
Re: Check which keyboard is typing [BARCODE SCANNER]
I concur: keys is keys at the point you are trying to snag them.
It has to work that way. Many devices can present themselves as USB keyboards (special keypads, even page scanner buttons) and the "keystrokes" are intermixed.
Some USB devices may present as a serial port, others as some custom or raw USB device. Those often need a DLL from the maker's SDK.
Some can even do both, but turning off the keyboard activity when you want to read from the custom device can be problematic. The "keyboarding" can resume spontaneously and wreak havoc on the user's desktop.
It is best to use a product where you have only non-key emulation, or at least one with a hard switch or option to deactivate keystroking.
-
Oct 23rd, 2017, 10:13 AM
#5
Re: Check which keyboard is typing [BARCODE SCANNER]
Add me to the list of "people who have never attempted this", but it may be possible:
https://www.codeproject.com/Articles...tiple-keyboard
That article uses C#, but it provides a list of the required APIs if someone wanted to attempt this in VB6.
-
Oct 23rd, 2017, 05:17 PM
#6
Re: Check which keyboard is typing [BARCODE SCANNER]
The KeyCode intercepted by KeyDown and KeyUp events is different if received from keyboard or barcode reader (it can be my KRAUN CCD barcode scanner...)
Try a small experiment:
create a New blank project, in Form1 add:
-1 TextBox (Text1)
-1 CommandButton (cmdClear)
-1 Checkbox (chkKB - Caption="Keyboard")
-2 ListBox (List1, List2, place side by side)
Paste this code
Code:
Option Explicit
Private Sub cmdClear_Click()
Text1.Text = vbNullString
List1.Clear
List2.Clear
End Sub
Private Sub Form_Resize()
If Me.WindowState = vbMinimized Then Exit Sub
With Me
List1.Height = .ScaleHeight - List1.Top
List2.Height = List1.Height
End With
End Sub
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
If chkKB.Value Then
List2.AddItem "--KeyDown" & KeyCode
Else
List1.AddItem "--KeyDown" & KeyCode
End If
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
If chkKB.Value Then
List2.AddItem "KeyPress" & KeyAscii
Else
List1.AddItem "KeyPress" & KeyAscii
End If
End Sub
Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
If chkKB.Value Then
List2.AddItem "--KeyUp" & KeyCode
Else
List1.AddItem "--KeyUp" & KeyCode
End If
End Sub
Run project,
1 - scan a barcode with reader on Text1 (List1 will fill)
2 - now set chkKB.Value to checked
3 - now type the same code usign kwyboard (list2 will fill)
Compare the two results...
Last edited by gibra; Oct 23rd, 2017 at 05:23 PM.
-
Oct 24th, 2017, 04:42 AM
#7
Lively Member
Re: Check which keyboard is typing [BARCODE SCANNER]
I think you may be out of luck here. At least not with out some really low level stuff going on...
... don't know if this may be helpfull, but have a look at HIDmacros (www.hidmacros.eu).
This small utility *can* differentiate between several keyboards and can use this to do different things. Outside of VB of course.
May be somewhat of a kludge, but it worked for me.
-
Oct 24th, 2017, 08:21 AM
#8
Re: Check which keyboard is typing [BARCODE SCANNER]
You can use `RegisterRawInputDevices` to listen for `WM_INPUT` messages, then access input data with `GetRawInputData` and filter on RIM_TYPEKEYBOARD + WM_KEYDOWN + HID device ID to differentiate b/n keyboard and barcode input.
Here is a working sample project: UsbBarcodeSanner.zip
Note that `WM_INPUT` messages are sent to the registered hWnd regardless if your app/window is active and has input focus at all (unlike `WM_KEYDOWN/UP`).
The sample above still lacks robust HID/USB device enumeration (no plaint-text device names) for UI purposes as RIDI_DEVICENAME contrary to it's name dumps device manager names like "\\?\HID#VID_0000&PID_0001#7&8bf8dff&0&0000#{884b96c3-56ef-11d1-bc8c-00a0c91405dd}".
cheers,
</wqw>
-
Oct 24th, 2017, 01:33 PM
#9
Junior Member
Re: Check which keyboard is typing [BARCODE SCANNER]
Some barcode scanner wedges allow the addition of a pre-amble (and post-amble) into the data stream. The pre-amble might be useable as a flag of sorts to mark the origin of the data (ie which keyboard wedge sent the data). Check your documents for the scanner.
Last edited by Bob_too; Oct 24th, 2017 at 01:46 PM.
-
Oct 25th, 2017, 01:34 AM
#10
Thread Starter
New Member
Re: Check which keyboard is typing [BARCODE SCANNER]
Originally Posted by wqweto
You can use `RegisterRawInputDevices` to listen for `WM_INPUT` messages, then access input data with `GetRawInputData` and filter on RIM_TYPEKEYBOARD + WM_KEYDOWN + HID device ID to differentiate b/n keyboard and barcode input.
Here is a working sample project: UsbBarcodeSanner.zip
Note that `WM_INPUT` messages are sent to the registered hWnd regardless if your app/window is active and has input focus at all (unlike `WM_KEYDOWN/UP`).
The sample above still lacks robust HID/USB device enumeration (no plaint-text device names) for UI purposes as RIDI_DEVICENAME contrary to it's name dumps device manager names like "\\?\HID#VID_0000&PID_0001#7&8bf8dff&0&0000#{884b96c3-56ef-11d1-bc8c-00a0c91405dd}".
cheers,
</wqw>
I think here is the solution. I'm working...
Originally Posted by Bob_too
Some barcode scanner wedges allow the addition of a pre-amble (and post-amble) into the data stream. The pre-amble might be useable as a flag of sorts to mark the origin of the data (ie which keyboard wedge sent the data). Check your documents for the scanner.
I can add pre an post, but I don't want to write the characters in the focused Window.
-
Oct 25th, 2017, 03:09 AM
#11
Re: Check which keyboard is typing [BARCODE SCANNER]
https://github.com/wqweto/UsbBarcodeSanner/releases
Did some cleanup and merged better HID enumeration -- now w/ human readable device names in the selection combo box.
cheers,
</wqw>
-
Oct 25th, 2017, 03:29 AM
#12
Thread Starter
New Member
Re: Check which keyboard is typing [BARCODE SCANNER]
Originally Posted by wqweto
Thanks!
In the next release can you add some comments to the methods?
-
Oct 25th, 2017, 03:41 AM
#13
Re: Check which keyboard is typing [BARCODE SCANNER]
Originally Posted by freaker
In the next release can you add some comments to the methods?
Highly unlikely as most of the code is excessive API calls and I don't have the time to rehash MSDN documentation on these.
If you do find time to comment on any methods implementation I'll be glad to accept your pull requests.
cheers,
</wqw>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|