9

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.

3
  • Demo codes: int devfd = open(device, ...); /* Open the kernel device "/dev/input/eventX". / read(devfd, ...); / Read a keyboard event. / write(devfd, ...); / Simulate a keyboard event. */ Commented Sep 1, 2012 at 18:35
  • 2
    No need to comment your own question. You can edit the question to include any further details, and format the source. Make sure your source is clear to those who will answer. Commented Sep 30, 2012 at 6:26
  • Use github.com/KarsMulder/evsieve Commented Mar 12, 2024 at 10:15

1 Answer 1

7

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.