What is the simplest way to (globally) bind a key combination (e.g. <Super>+A
) to a function in a gnome shell extension?
Inspecting a couple of extensions, I ran into the following code:
global.display.add_keybinding('random-name',
new Gio.Settings({schema: 'org.gnome.shell.keybindings'}),
Meta.KeyBindingFlags.NONE,
function() { /* ... some code */ });
I understand that the key combination is specified by the schema parameter, and that it's possible to create an XML file describing the combination. Is there a simpler way to do this?
Following is a copy of my answer here I've only tested this in Gnome 3.22
TL;DR
Here is a class:
And that's how you you use it:
You're going to need imports:
Explanation
I might be terribly wrong, but that what I've figured out in last couple days.
First of all it is Mutter who is responsible for listening for hotkeys. Mutter is a framework for creating Window Managers, it is not an window manager itself. Gnome Shell has a class written in JS and called "Window Manager" - this is the real Window Manager which uses Mutter internally to do all low-level stuff. Mutter has an object MetaDisplay. This is object you use to request listening for a hotkey. But! But Mutter will require Window Manager to approve usage of this hotkey. So what happens when hotkey is pressed? - MetaDisplay generates event 'filter-keybinding'. - Window Manager in Gnome Shell checks if this hotkey allowed to be processed. - Window Manager returns appropriate value to MetaDisplay - If it is allowed to process this hotkey, MetaDisplay generates event 'accelerator-actived' - Your extension must listen for that event and figure out by action id which hotkey is activated.