Window Managers and XGrabKey*()

Glynn Clements glynn at
Fri May 29 07:46:57 PDT 2009

Bedő Sándor wrote:

> I have an academic question about X Window Managers.

> So I think the idea of grabbing the keyboard is wrong...


> 2)  My next idea was to grab only the desired keys with the
> Xlib function XGrabKey().  This is the way I implemented
> my Window Manager, and it works fine with a couple of
> other X clients, like xterm, or firefox.  But there are a
> few other clients, that use XGrabKeyboard, and in case these
> applications get the focus, and appropriate the keyboard,
> my Window Manager does not get the desired keystrokes.
> Hmmm, maybe grabbing the whole keyboard in the Window
> Manager is not such a wrong idea?  Clients usually don't care
> about the return value of the XGrabKeyboard() call (as I
> can see in the source of a few apps), so they always would
> think that they have the keyboard grabbed, even if they
> did not.
> How it is possible to implement an always working window
> switching key in my Window Manager, even if a client grabs
> the keyboard?  Maybe there is an Xorg extension, or a tricky
> Xlib function, that makes it possible...  Or the problem is
> that I've read an ancient documentation about the Xlib?

No, your goal is wrong. If a client calls XGrabKeyboard(), it should
receive all keystrokes, even the ones which your WM would normally
process. You just need to accept that WM keystrokes won't
work while the keyboard is grabbed.

E.g. if the grab is made by a screensaver which requires a password to
unlock the display, it's rather important to ensure that someone can't
just walk up to the system and bypass the screensaver via WM hotkeys.

Glynn Clements <glynn at>

More information about the xorg mailing list