Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to comsume a keyboard event on Linux using input subsystem

I am writing a Linux program in C, and I need to intercept certain keyboard strokes.

Using the input subsytem (read/write /dev/input/eventX), I can receive a keyboard stroke (using "read" functions) or simulate a keyboard stroke (using "write" function).

When using "read" function, I can capture the user keyboard strokes, but this event is propagated and I don't know how to consume it.

like image 273
Chip Zhang Avatar asked Sep 01 '12 18:09

Chip Zhang


1 Answers

By default, input events are transmitted to all listening applications and drivers. It is possible, however, to have an application grab the device via the evdev interface - have a look at the EVIOCGRAB ioctl(). That would only allow that specific application to receive events from that particular device.

The problem with that approach is that you cannot actually prevent a specific event from being propagated after it is received - you can only grab the device beforehand, which would then capture all events. Therefore, if you want to filter input events you have to use a workaround.

The workaround that I used in my own evmapd daemon involved grabbing the original device and using the uinput subsystem to provide another device with all the modifications that I needed, including remapped keys and various other changes...

like image 162
thkala Avatar answered Sep 27 '22 22:09

thkala