How to use a USB HID device by only one Python process?

1.9k Views Asked by At

I'm developing a background application with use of a barcode scanner. The barcode scanner is recognised as an USB HID (Human Interface Device) device on my linux OS. So the default USB HID driver is loaded and the scanner behaves like a keyboard.

I'm able to read the STDIN contents of the scanner by using the pyxhook Python module. So even if my process is running in background, I can read the input from the barcode scanner.

My problem is, that the background application is also reading the inputs from the regular keyboard, because it also writes into STDIN.

I would like to use the STDIN of the barcode scanner by the background process only and ignore the STDIN of other HID devices such as keyboard, but I have no clue how to realize that. Any idea?

1

There are 1 best solutions below

0
On

If you intercept the input on a lower level (evdev), you can split it out by device. For python, there is python-evdev. You can filter devices based on name, and go from there. One downside is that you have to translate the evdev events to readable input yourself (something the kernel does for you normally).

I've written a simple program for saving barcodes to a file, to run on a RPi (with additional support for GS1-128/SSCC-128/EAN-128).