Linux – “Bind” USB-keyboard exclusively to specific application

headlesskeyboardlinux

What i have:

  • Linux machine (debian stable) without monitor, keyboard, no X running
  • USB-device which acts like an USB-keyboard (like many barcode-readers [1])

What i (don't) want:

  • I want to use this device only for one specific application.
  • I don't want to have this device interfere with e.g. the login process after booting or anything else – except the one application.
  • In any way, i want to be able to plug in a plain old USB keyboard an use it as such – independent of how many other USB-devices like mentioned above may be connected or not.

What i (don't) know:

  • I found some example codes, about how to read directly from the event devices and can use this in my application, if needed.
  • I don't know, how to disable a specific keyboard for the whole system except this specific application? Is this even possible?

Thanks for any hints … !

[1] When the device reads a barcode, it's sends the detected code as single key presses and commits with return.

Best Answer

A challenging and interesting question!

I think udev might be capable of doing just that. Create a file /etc/udev/rules.d/99-barcode-reader.rules and put there something similar to this:

ACTIONS!="add", GOTO="barcode_end"
SUBSYSTEM=="usb", ENV{DEVTYPE}=="usb_device", ATTRS{idVendor}=="dead", ATTRS{idProduct}=="beef", PROGRAM="/bin/sh -c 'logger -p user.info Hey, I see a new device $env{BUSNUM} $env{DEVNUM}'", RUN+="/bin/sh -c '/usr/local/bin/your_barcode_application &'"
LABEL="barcode_end"

Replace "dead" and "beef" with the values you get for idVendor and idProduct with lsusb.

Restart udevd or simply your server, and see what happens.