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.
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 theEVIOCGRAB
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 theuinput
subsystem to provide another device with all the modifications that I needed, including remapped keys and various other changes...