Search code examples
javaeventsactionbarcode-scannerscanning

Reading data from a barcode scanner using global hooks


I have an USB barcode scanner and I created a reader with java, using this library: https://github.com/kwhat/jnativehook

My reader uses global hooks and detects when the data comes from the barcode scanner based on the speed it is typed. This is actually very accurate. This reader works in background. When I read any barcode is does some stuff with it and registers some values (that are not relevant for the question) on a database.

It happens that with the hooks, when I scan something they keys events are caught by the focused program (chrome, word, etc) and the numbers on the barcode are displayed on it. This is really annoying if you are trying to write a document or something while another person is using the scanner.

Is there anyway, using this or another java library to prevent the keyboard events from propagate? I want to catch all keyboard events, consume them, and if my application detects they are not coming from the barcode scanner then it would re-create the event and send it to the other programs to catch.

Thanks.


Solution

  • You can consume events with JNativeHook if you are using Windows or OS X. The feature isn't supported on Linux so it is not an officially supported feature. See: https://github.com/kwhat/jnativehook/wiki/ConsumingEvents