5

It appears it should be relatively easy to set Super+C to launch my GNOME calculator in Ubuntu 22.04 with gsettings and the command line:

gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom999/ name "'Launch Calculator'"
gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom999/ binding "'<Super>c'"
gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom999/ command "'gnome-calculator'"

I issue the command, everything appears to work fine, then I press Super+C and nothing happens. What did I do wrong?

This will be part of a script, so I can't use any GUI solutions, although we could use the GUI (dconf, settings, etc.) for troubleshooting. In fact, that's what I've done and I can see the shortcut listed in my

dconf > org > gnome > settings-daemon > plugins > media-keys > custom-keybindings

however, it is missing from my

Settings > Keyboard > Keyboard Shortcuts > View and Customize Shortcuts

that seems to be a symptom of why "nothing happens" when I press Super+C.

jophuh
  • 373

1 Answers1

4

Each custom directory (ending with /) you add under the directory

/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/

(which is a directory ending with /) needs to have its path added as a value entry to the key

/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings

(which is a key with the same name as the above mentioned directory but not ending with /)
... and you can read its current values with

dconf read /org/gnome/settings-daemon/plugins/media-keys/custom-keybindings

So, in addition to creating the custom directory and populating it with keys and values like this (using dconf):

dconf write /org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom999/binding "'<Super>c'"
dconf write /org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom999/command "'gnome-calculator'"
dconf write /org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom999/name "'Launch Calculator'"

... which you can verify with dconf dump like this:

$ dconf dump /org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom999/
[/]
binding='<Super>c'
command='gnome-calculator'
name='Launch Calculator'

... You will need to add its path to the /org/gnome/settings-daemon/plugins/media-keys/custom-keybindings key while preserving the pre-existing values in that key's array ['...', '...', ...] like this:

if [ -z "$(dconf read /org/gnome/settings-daemon/plugins/media-keys/custom-keybindings | tr -d '][')" ]; then
  dconf write /org/gnome/settings-daemon/plugins/media-keys/custom-keybindings "['/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom999/']"
  else
  dconf write /org/gnome/settings-daemon/plugins/media-keys/custom-keybindings "[$(dconf read /org/gnome/settings-daemon/plugins/media-keys/custom-keybindings | tr -d '][') , '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom999/']"
fi

... which you can verify with dconf read like this:

$ dconf read /org/gnome/settings-daemon/plugins/media-keys/custom-keybindings
['/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom1/', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom999/']

Notice I used the dconf tool as it's available by default on all Ubuntu installs and thus offers out-of-the-box support especially for widely deployed scripts, but you can install the gsettings front-end tool and use it modifying the above commands' syntax and directory/key paths style to work with it ... See man gsettings and look at the output of gsettings list-schemas and gsettings list-recursively before using it.

rubo77
  • 34,024
  • 52
  • 172
  • 299
Raffa
  • 34,963