"Bind" USB-keyboard exclusively to specific application

Solution 1:

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.

Solution 2:

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