5

I'm trying to implement a simple X11 key grabber in C for window switching with Alt-Tab etc. I can use the function XSelectInput to handle keyboard events for a specific window:

XSelectInput(display, window, KeyPressMask | KeyReleaseMask);

How can I receive "global" keyboard events regardless of which window is focused?

August Karlstrom
  • 10,773
  • 7
  • 38
  • 60
  • Take a look at my c program of hooking mouse and keyboard globally in X11 [link](https://github.com/BladeMight/Mahou.linux/blob/master/X11-Hooks.c). – BladeMight Aug 12 '17 at 14:24
  • Also, check the source code for `xev`: http://xev.sourcearchive.com/documentation/1:1.0.2-0ubuntu1/xev_8c-source.html `xev` will show *all* X events, including every key press event. – Andrew Henle Aug 12 '17 at 14:25

1 Answers1

3

You need XGrabKey. This function is specifically designed for implementing hotkeys.

When the desired key combination is pressed, you get the event no matter what, and no other window gets the event.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
  • what if you want to be notified but not steal it from other clients? There's the alternative of following the focussed window without grabbing but this approach seems very unreliable – phil294 Jul 15 '22 at 15:19
  • @phil294 [here you are](https://stackoverflow.com/questions/22749444/listening-to-keyboard-events-without-consuming-them-in-x11-keyboard-hooking) – n. m. could be an AI Jul 15 '22 at 16:43