How to comsume a keyboard event on Linux using inp

2019-03-28 18:16发布

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.

1条回答
Emotional °昔
2楼-- · 2019-03-28 19:05

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

查看更多
登录 后发表回答