on Linux, how do I find out which application/processid is receiving keyboard/mouse input?

I am working on a linux project. I am stuck at a point where I need to know which application/processid is receiving keyboard/mouse input. I mean that binding must be stored somewhere. Can somebody help me out? Edit 1: I am working on a keyboard/mouse event capture project. I have found logkeys (project) useful. I need to find application name to which user is giving input along with the keys pressed Edit 2: I am using CentOS 7. XDG_SESSION_DESKTOP = gnome-classic. GDMSESSION = gnome-classic.

2 answers

  • answered 2018-05-16 07:26 user2699113

    The linux kernel is responsible for receiving events from physical keyboard or mouse not any particular application. And then the events are passed to active application. So look for drivers for keyboard and mouse in kernel sources.

  • answered 2018-05-16 07:29 Basile Starynkevitch

    I need to know which application/processid is receiving keyboard/mouse input.

    In practice, on a Linux desktop (or laptop), it does not really matter (see below why), if you think of the physical keyboard & mouse.

    On a Linux server, you often don't have any physical keyboard.

    On a Linux desktop or laptop used by some physical person, you generally have some display server (e.g. Xorg or Wayland) for the graphical desktop environment (unless you use virtual consoles without any GUI). That display server is handling (and reading from) the physical keyboard and mouse, so is (in practice) the only process reading from them. Check with lsof(8).

    Of course, the display server is processing physical keyboard and mouse events, and making out of them higher-level messages sent to some client and window (with the help of some window manager, and managing the focus). But how that happens is a different question (and is very different in e.g. Xorg and in Wayland). Read also about compositing window managers. BTW Xorg clients might run on remote machines.

    A Linux machine could manage several seats, that is several combinations of screen(s)+keyboard+mouse each used by a different physical person. Then you could have several display servers.

    You could spend months or years studying X11 protocols and architecture. The documentation is heavy: many thousands of pages (see also ICCCM & EWMH). And you'll spend also many months studying Wayland protocols, if you need to.

    You could need many years of work (or even a lifetime) for your project. The cumulated software layers (display server, GUI toolkits, window manager, etc...) are huge, several dozens of millions of source code lines.

    See also this answer to a very similar question, and this

    If you are using gnome-classic your display server is certainly Xorg. So ICCCM & EWMH apply. Then you might be interested in _NET_WM_PID and WM_CLIENT_MACHINE in conjonction with usual techniques to get the X11 focus window. You may consider patching the usual window manager (and you probably still need to read a lot more about X11 to be able to code some robust implementation).

    Don't forget that many X11 client applications are opening several top-level X11 windows, and that some few X11 client applications are using several X11 displays, so several Xorg display servers.