linux – How to disable keyboard but still allow reading from it

keyboardkeyboard-layoutlinux

I have a USB numpad that I want to use to control a specific application running as a daemon in the background. The daemon is written in Python and I'm currently reading input using python-evdev which works quite well.

However, everything I type on the keyboard is still also processed normally, meaning that keypresses are also inserted into any application dealing with keyboard input (including the login prompt that is shown when the computer boots up). This is somewhat annoying.

Is there a way to disable the "normal" processing of keyboard events, and only allow manual reading of the key states? Hopefully one that does not depend on running X.

Everything I've found so far seems to be dealing with disabling the keyboard completely or using X.

One idea I have is to create a keymap which maps all the keys to dead keys, which prevents any output, but still allows me to read the actual keycodes. However, it seems that there should be an easier solution to this problem.

Best Answer

If I understand the kernel sources directly, you cannot disconnect specific input devices from the global handlers (see /proc/bus/input/handlers): The kbd handler will always receive all input events, and convert key events into keypresses.

However, you can grab an input device for exclusive use with an EVIOCGRAB ioctrl on the device, either directly from your program, or using tools such as evtest --grab /dev/input/eventX (for testing). As long as the grab is active, the events shouldn't be processed by anything else.

I'm not familiar with python-evdev, but even if it doesn't support grab mode, it's not to difficult to perform ioctls in Python.

(I was lazy and tested only under X, where it works, but I see no reason why it shouldn't work without X).

Related Question