r/C_Programming • • Jun 03 '26

Question Get ALL keyboard input from Linux?

I'm currently making a program where when a key is pressed on any window or screen, a specific action happens, right now I am reading from /dev/input/event with open() but the problem is

  1. It only reads from a very specific device
  2. It doesn't read from all "keyboards" that I have (I have a laptop keyboard and a wired keyboard) and
  3. Sometimes the main keyboard that I use will just switch up it's number and I have to recompile the thing again

Is there a way to just conveniently get all keyboard input without all this hassle?

10 Upvotes

13 comments sorted by

View all comments

Show parent comment

1

u/Wertbon1789 Jun 05 '26 edited Jun 05 '26

No, that's not quite right if we're talking about the intended way, and not what OP wanted to do. You can get input via the Wayland protocol, specifically via the wl_seat interface. wl_seat is implemented by the specific compositor and in general they only send your app keystrokes if you're the focused app. If you want to listen for every single keystroke you might need to go through udev, sidelining the compositor, but if you're just a normal (actually non-malicious cough) app you're supposed to go through wl_seat. X11 never had such a things and basically just vomited out keystrokes to every app, AFAIK, and you had to handle if you're interested in them.

Easy to verify, just look at any Wayland app's open file descriptors, there are no input devices that are just randomly opened by any Wayland app.

EDIT: Even easier way, just start an app, like a terminal, with WAYLAND_DEBUG=1, this will dump every event that the compositor and the application send to each other.