“Bind” USB-keyboard exclusively to specific application

Posted on

“Bind” USB-keyboard exclusively to specific application – A server stack is the collection of software that forms the operational infrastructure on a given machine. In a computing context, a stack is an ordered pile. A server stack is one type of solution stack — an ordered selection of software that makes it possible to complete a particular task. Like in this post about “Bind” USB-keyboard exclusively to specific application was one problem in server stack that need for a solution. Below are some tips in manage your linux server when you find problem about linux, keyboard, headless, , .

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.

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.

I dunno if it’s what you want to do but if you’re trying to disable the keyboard emulation that output barcode sequences in stdin/console just try this:

Create a file “/etc/udev/rules.d/10-barcode.rules”

SUBSYSTEM=="input", ACTION=="add", ATTRS{idVendor}=="xxxx", ATTRS{idProduct}=="yyy", RUN+="/bin/sh -c 'echo remove > /sys$env{DEVPATH}/uevent'"
ACTION=="add", ATTRS{idVendor}=="xxxx", ATTRS{idProduct}=="yyyy", SYMLINK+="barcode"

Of course, replace xxxx and yyyy with your own vendor_id and product_id, and reload the rules with “udevadm control –reload”

This will remove the event/input part that is emulating keyboard and keep the hidraw part to access barcode scans. The second line just create a /dev/barcode device to read linked to the hidraw

Just open that file in your code to read strings sent by the barcode

For what it’s worth and for anyone with a similar problem finding this;

I don’t know if it’s possible to disable a “keyboard”, but since the system is headless anyway, you can just run a service to handle the input in TTY1, replacing the normal login prompt. You can still login normally via ssh or any other TTY, but on boot, by default, the barcode reader input is handled properly without additional action.

A nice bonus is to run all of that inside a screen session, so you can connect to it using ssh.

For instructions, check out my answer on SU here.

Leave a Reply

Your email address will not be published.