Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gnome Shell Extension Key Binding

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?

like image 262
user1655742 Avatar asked Sep 07 '12 21:09

user1655742


1 Answers

The question is old, but I just implemented that for Gnome Shell 40. So here is how I did it.

The key is defined in your normal schema file that you use for the settings of the extension. So it looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<schemalist>
    <schema id="org.gnome.shell.extensions.mycoolstuff" path="/org/gnome/shell/extensions/mycoolstuff/">
        <key name="cool-hotkey" type="as">
            <default><![CDATA[['<Ctrl><Super>T']]]></default>
            <summary>Hotkey to open the cool stuff.</summary>
        </key>
        
        ... other config options

    </schema>
</schemalist>

The key type is a "Array of String", so you can configure multiple key-combinations for the action.

In your code you use it like this:

const Main = imports.ui.main;
const Meta = imports.gi.Meta
const Shell = imports.gi.Shell
const ExtensionUtils = imports.misc.extensionUtils;

...

let my_settings = ExtensionUtils.getSettings("org.gnome.shell.extensions.mycoolstuff");

Main.wm.addKeybinding("cool-hotkey", my_settings,
    Meta.KeyBindingFlags.IGNORE_AUTOREPEAT,
    Shell.ActionMode.NORMAL | Shell.ActionMode.OVERVIEW
    this._hotkeyActionMethod.bind(this));

I would recommend to remove the key binding when the extension gets disabled. Don't know what happens if you don't do this.

Main.wm.removeKeybinding("cool-hotkey");

BTW: Changes to the settings (via dconf editor, gsettings or your extensions preferences) are active immediately.

like image 158
Ralf Avatar answered Oct 30 '22 06:10

Ralf