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 a programmer on Windows and want to implement some feature on Linux it almost always makes 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 a 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 the state of a 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 the code more universal. In that case, we'll create an extended class of TThread with our needs.

Actual code

And now we can use our unit like this: