6

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.

3
  • Which kind of "the login prompt that is shown when the computer boots up" are you talking about?
    – andcoz
    Commented Feb 7, 2017 at 23:11
  • On the console. I'm not running X, so the "duplicate" question is not a duplicate. In the question I specifically mentioned not wanting to depend on X, while that question specifically asks about X.
    – pafcu
    Commented Feb 8, 2017 at 7:08
  • Ops, sorry. You are right. I voted for question re-open.
    – andcoz
    Commented Feb 8, 2017 at 10:32

1 Answer 1

8

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).

3
  • I guess MagicSysRq won't be ignored despite the keyboard being grabbed, right?
    – Ruslan
    Commented Sep 7, 2019 at 16:24
  • @Ruslan: I would assume so, too, but I didn't test it.
    – dirkt
    Commented Sep 7, 2019 at 18:18
  • 1
    Just tested, both in VT1 and X. Amazingly, it doesn't pass even Magic SysRq from the keyboard it grabbed, while from another one I was able to trigger Emergency Sync and Help. So it seems your solution is quite a strong one.
    – Ruslan
    Commented Sep 7, 2019 at 18:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .