Search code examples
javac++inputkeyboardjava-native-interface

Capturing keyboard input from specific USB port


i'm stuck at capturing keyboard events.

I'm working with a barcode scanner which is detected by system as traditional keyboard (it is it's only mode, my client has a lot of budget scanners which can't emulate virtual com port).

My goal is to be able to detect at which port is this scanner plugged in, capture any input from that port, and prevent it from being handled by the OS (i don't want the barcode numbers popping up on focused input).

All this must be performed by a background service, which is never an active window, AND the solution must work both on linux and on windows.

I've been able to capture the input using jnativehook, but failed to prevent it from being handled by OS.

As far as i know, it is impossible to achieve this using pure java (JVM gives access to keyboard events only when application window is focused), so how should i handle this issue? I'm okay with using jni and c++ if that's necessary, but i don't know where to start.


Solution

  • JNativeHook does have the ability to discard events on Windows and OSX (not Linux), however, it does not have the ability to determine which USB port the scanner is connected to. The reason it cannot consume events on Linux is because the XRecord API makes a copy of the events and doesn't sit directly on the input event loop. If the devices you are working with can utilize HID, I would take a look into some of the JNI HID library wrappers like gary-rowe/hid4java or signal11/hidapi. The assertion that "it is impossible to achieve this using pure java" is correct. The only way to do it would be though native code, and HID is the most appropriate native method to accomplish your goals. Other input methods like input hooking (used by JNativeHook) will not be able to provide you with the source USB port, nor can they consume events on some platforms (Linux). There is another option out there like melloware/jintellitype that use a different mechanism for capturing input. I don't think it can grab input based on the port, however, the non-portable Linux equivalent of this library uses the XGrab API which will only consume events, but again, I don't know if it will be practical for your input source as I think it can only bind one key per call and that key must use a modifier.

    Hope this helps you move forward with your project.