5

I am using a barcode scanner connected to Windows PC. It is configured to work as USB HID device, and it is the most reliable mode for this scanner to work. I have developed some software to register input from it. The thing is that I want to have some program running in background and intercepting data from barcode scanner and sending the obtained data over network. I have checked some similar solutions, but seems like that people mostly go with having a background window.

Reading USB HID barcode scanner input without knowing VID&PID - here is a similar question, but the answer describes a swing KeyListener child for Java GUI form https://stackoverflow.com/a/14106511 - also points to similar idea.

It is an app for internal usage, so I may even manually specify VID and PID of device.

Is there any way to read and intercept input from barcode scanner without GUI window?

Ivan P.
  • 832
  • 2
  • 9
  • 26
  • Most barcode scanners I've worked with have simulated keyboard input. You could probably use raw input to read it "in the background" but you might find it clobbers your "foreground" input anyway. – Jonathan Potter Apr 19 '18 at 05:30
  • 1
    You would need a combination of Raw Input and a low-level keyboard hook. The former can identify the hardware source of the input, but can only monitor it. The latter cannot identify the source, but is capable of intercepting input, preventing the system from passing it on to the target window. A proposed solution would store the input from the device in a list from a Raw Input handler, while a low-level keyboard hook processes the input, if it matches the first item in the list, and subsequently removes it. Since all of the code runs on the same thread, you do not need any synchronization. – IInspectable Apr 19 '18 at 07:34
  • @IInspectable note that coordinating a Raw Input hook with a Keyboard hook is not trivial or reliable, full of gotchas and caveats: [Combining Raw Input and keyboard Hook to selectively block input from multiple keyboards](https://www.codeproject.com/Articles/716591/Combining-Raw-Input-and-keyboard-Hook-to-selective) – Remy Lebeau Apr 19 '18 at 08:23
  • @RemyLebeau: It certainly isn't trivial. What you can do is to check for the `LLKHF_INJECTED` in the low-level keyboard hook. This allows you to distinguish between injected input and keyboard originating from a genuine keyboard device. This is based on the assumption, that the scanner doesn't register as a keyboard device (which could be wrong). You can also try to correlate the timestamps of the `WM_INPUT` messages and the keyboard messages in the hook procedure. That, too, can produce false positives as well as false negatives. A reliable solution likely requires a custom driver. – IInspectable Apr 19 '18 at 08:51
  • @IInspectable from the article I linked to: "*When I was first experimenting with the combination of the APIs, I tried to use a global Low Level Keyboard Hook (WH_KEYBOARD_LL). The problem is, when we use the Low Level Keyboard Hook to block some input (we stop the progress of the message), **Windows won't generate the Raw Input event**, meaning no application will get the appropriate Raw Input message (WM_INPUT). Because of that, **we can't use Low Level Keyboard Hook**, but we have to use a standard Keyboard Hook (WH_KEYBOARD), which is a bit harder to set up.*" – Remy Lebeau Apr 19 '18 at 16:10
  • @IInspectable "*A reliable solution likely requires a custom driver.*" - agreed. – Remy Lebeau Apr 19 '18 at 16:11
  • Some barcode scanners provide alternative APIs to receive input, rather than simulating keyboard input through UI messaging. Look at the documentation for your particular scanner to see what it offers. – Remy Lebeau Apr 19 '18 at 19:27

0 Answers0