Linux – How to prevent the keyboard from being interpreted by the current application

keyboardkeyboard-eventlinuxudevx11

I have 3 USB-keyboards attached to the Linux. Normally, when writing on any one, the characters "going" to the currently active application, e.g. to terminal or any other app.

But, me reading the keyboard events from two keyboards directly by reading the particular /dev/input/eventX devices using some perl module. While the script reads and decodes all events correctly, the entered keys are also going into active application.

The question is: It is possible to stop routing the keyboard events from particular keyboard to the active application? E.g. somewhat tell to kernel that inputs from particular keybord should not be taken as normal keyboard input.

EDIT

because the X11 reads from every device which has device nodes in the /dev/input/event* – kooks like, here are 2 possible solutions:

  1. Somewhat change the name of the USB "keyboard" to another name instead of eventN, for example /dev/input/myinput0. Unfortunately, the udev rules doesn't allows renaming the device. (The NAME directive in the rules.d works only for network devices, for other devices could create only symlink)
  2. Somewhat change the X11 configuration, in the /usr/share/X11/xorg.conf.d/10-evdev.conf, to ignore some particular "keyboard-like" devices – e.g. don't read every eventN device. Currently in my system it contains:
Section "InputClass"
        Identifier "evdev keyboard catchall"
        MatchIsKeyboard "on"
        MatchDevicePath "/dev/input/event*"
        Driver "evdev"
EndSection

ANY IDEA how to do any of the above?


The real background: I have attached two USB-RFID readers. They acts as keyboards, e.g. when I touching the reader with the RFID-tag, it sends the RFID-number exactly as they was typed on the keyboard – e.g the readers acts as normal keyboard.
My application could read the RFID events (in the background), and (of course) i don't want get the characters from RFID into the active window.

Best Answer

If you are reading from /dev/input/eventX anyway, just do an EVIOCGRAB ioctl on it. You can issue ioctl's in Perl easily. Don't forget to release the grab when your program quits.

The grab will prevent all other devices, including X, from reading events from this device.

That's cleaner than xinput, because you also exclude other applications that may want to read directly from the device, and you also can control the duration of the exclusion (as long as your application runs).

Related Question