0

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.

  • You may want to look at [Linux / Unix: Find Out What tty I’m Using](https://www.cyberciti.biz/faq/linux-unix-appleosx-bsd-what-tty-command/) – David C. Rankin May 16 '18 at 05:12
  • @DavidC.Rankin tty works only when input is going to a terminal window. I need something broad. That is working even if I am giving input to a chrome or excel applications. – Nikhil Nilawar May 16 '18 at 05:45
  • 1
    `chrome` and `excell` will be running under a "desktop" where the "focus model" will determine which "window" is receiving keyboard input. A desktop will implement a "virtual keyboard" which will have the keyboard tty bound to it (generally `/dev/console`), but beyond that it will be up to the window manager and desktop to direct input to the appropriate window/process. Hopefully someone else can expand on it -- but you will need to describe which desktop you are using. You can launch `xev` to look at the X-input handler. – David C. Rankin May 16 '18 at 06:02
  • Why is this information important? – Ignacio Vazquez-Abrams May 16 '18 at 06:09
  • I am using CentOS 7. XDG_SESSION_DESKTOP = gnome-classic. GDMSESSION = gnome-classic. – Nikhil Nilawar May 16 '18 at 06:16
  • @IgnacioVazquez-Abrams 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. – Nikhil Nilawar May 16 '18 at 06:25
  • In other words -- you want to create a keyboard/mouse sniffer? – David C. Rankin May 16 '18 at 07:38
  • 1
    @NikhilNilawar: Don't comment your own question but **edit your question** to improve it – Basile Starynkevitch May 16 '18 at 07:41
  • How much years of work can you afford spending? – Basile Starynkevitch May 16 '18 at 08:07
  • I find it actually an interesting question. Basile's answer is discouraging but one could prune some dozen milion lines of code by programming for a bare console (no X server running). E.g. a console vim can be made to use mouse events for selecting and cursor positioning. I suppose that vim does not open a mouse device directly but gets pre-processed events from an intermediate layer like a tty or whatever. But that amount of complexity should be more "handleable". And yes, the kernel keeps links between files and processes using them (see [lsof](https://linux.die.net/man/8/lsof)) – Peter - Reinstate Monica May 16 '18 at 08:35
  • I find that question lacking even a few minutes of research.... So not so interesting. In its current state, it is really too broad and unclear. And nearly duplicate of [this one](https://stackoverflow.com/q/12744064/841108) – Basile Starynkevitch May 16 '18 at 08:38

2 Answers2

0

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.

user2699113
  • 4,262
  • 3
  • 25
  • 43
0

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.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • But technically I suppose that an application (I'm unsure there but probably X, or in a console some pseudo tty?) opens a device and reads from (respectively `select`s on) it. That would be the techically straight-forward answer. I'm not positive -- can more than one process open and read e.g. from the mouse device at any given time? Is it possible to see processes which have opened a device (I should think so)? – Peter - Reinstate Monica May 16 '18 at 08:25
  • Having several processes reading the mouse might be possible, but is practically useless. Only the display server is reading the mouse – Basile Starynkevitch May 16 '18 at 08:25