Global shortcut hook on linux

Linux global shortcut


Global shortcut term means that you can set certain operation on some keyboard combination wherever you are in the system.

When you are programmer on Windows and want to implement some feature on Linux it almost always make you angry. Just because they are very different systems. In this article I'll tell about how to implement global hotkeys on Linux. Maybe I'll save your time :)

Where to start

In Unix systems there is no internal hook system for keyboard. So you can't just register / unregister global hotkey. But on linux you can use XServer to implement the hook

In common we need two functions: XGrabKey and XUngrabKey. Our program has to monitor state of keyboard. And it will do it in a while-cycle so we have to create also thread for this operation.

Also you might want to make code more universal. In that case we'll create extended class of TThread with our needs.

Actual code

And now we can use our unit like this: