1

Similar to this question from 2012, I'm looking for a way to automate the setup of a key combinations that should trigger the execution of a command the same way as setting it in the gui does. (example usage)

I'm on Ubuntu 18.04 and the linked answer's gsettings schema does not seem to exist:

$ gsettings list-schemas | grep keyboard
org.gnome.settings-daemon.plugins.keyboard
org.gnome.settings-daemon.plugins.a11y-keyboard
org.gnome.shell.keyboard
org.gnome.settings-daemon.peripherals.keyboard
org.gnome.libgnomekbd.keyboard
com.canonical.indicator.keyboard
org.gnome.desktop.peripherals.keyboard
org.gnome.desktop.a11y.keyboard

How can I use bash to set it up as if I went in the GNOME GUI to Settings > Devices > Keyboard and set it up manually? example image showing the setup in the GUI

lucidbrot
  • 1,250
  • 3
  • 16
  • 36
  • 1
    Not quite easy. You need to declare a folder for the shortcut key in org.gnome.settings-daemon.plugins.media-keys custom-keybindings, then create said folder under /org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/. In that folder, keys "binding", "command" and "name" define the custom shortcut key. Not sure how that is easily done with commands (especially first step). – vanadium May 01 '20 at 16:35
  • Should `gsettings list-keys org.gnome.settings-daemon.plugins` show `media-keys` if I already have set up some shortcuts with the gui? Because it only lists `emon.plugins` and `whitelisted-plugins` @vanadium – lucidbrot May 01 '20 at 16:43
  • Nvm, it does show up with `gsettings list-recursively org.gnome.settings-daemon.plugins | grep media` – lucidbrot May 01 '20 at 16:45
  • I will be looking into [this answer](https://askubuntu.com/a/428869/606260) which seems applicable and which I found thanks to your help – lucidbrot May 01 '20 at 16:52
  • @vanadium Nevermind - I figured it out I think. I will write an answer after more testing – lucidbrot May 01 '20 at 20:58

3 Answers3

1

After the comment

Not quite easy.
You need to declare a folder for the shortcut key in org.gnome.settings-daemon.plugins.media-keys [called] custom-keybindings, then create said folder under /org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/.
In that folder, keys "binding", "command" and "name" define the custom shortcut key. Not sure how that is easily done with commands (especially first step). – vanadium

I have looked at the following links:

My laptop on which this is already set up using the GUI, I ran a few queries to know what output I should aim for.

$ gsettings list-relocatable-schemas | grep keybin
org.gnome.settings-daemon.plugins.media-keys.custom-keybinding
org.gnome.orca.keybinding

## I have two shortcuts
$ gsettings get 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/']

$ gsettings get org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom1/ binding
'<Primary><Super>Left'

$ ls /opt
confinedrv  virtio-win.iso  workspace_shortcut_send_to_next.sh
generic@motorbrot:~/Downloads/iso.new$ gsettings get org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom1/ command
'wmctrl -r :ACTIVE: -t 0'

$ gsettings get org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom1/ name
'send to first workspace'

On my testing device (different from the previous laptop), querying org.gnome.settings-daemon.plugins.media-keys custom-keybindings returned an empty string array (@as []).

On my testing device, I tried to set a binding:

$ gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom1/ binding '<Primary><Super>Right'
No such schema "org.gnome.settings-daemon.plugins.media-keys.custom-keybindings"
'<Primary><Super>Left'

In /usr/share/glib-2.0/schemas/org.gnome.settings-daemon.plugins.media-keys.custom-keybindings.gschema.xml on the testing device is a schema defined for org.gnome.settings-daemon.plugins.media-keys.custom-keybinding (without trailing s). There I can see that the types of name, binding, and command are all s for string. So I dropped the trailing s in the id but not the path and now setting and querying them again works.

$ gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/ binding '<Primary><Super>Right'
$ gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/ name 'move to first wirkspace'
$ gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/ command 'wmctrl -r :ACTIVE: -t 0'
$ gsettings get org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/ binding
'<Primary><Super>Right'

I then added my custom command path to the previously empty list. (I think this overwrites the whole list, so beware!)

gsettings set org.gnome.settings-daemon.plugins.media-keys custom-keybindings "['/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/']"
## make sure the path starts and ends with a slash
gsettings get org.gnome.settings-daemon.plugins.media-keys custom-keybindings 
"['/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/']"

I proceeded to reload gnome by entering r into the popup that comes when I press AltF2.

I moved a window to a second workspace and tried whether it works. It does not - nothing happens.
I changed the command to echo test > ~/testing.txt and tried again, but still - nothing happens. The file is not created.

The problem here was the same as in my linked example usage with the GUI:

If the shortcut does not work in that way, but the command works when run from the terminal, save it in a file /fullpath/myfile.txt and set bash /fullpath/myfile.txt as the shortcut command. That should work both on Gnome and Xfce.

lucidbrot
  • 1,250
  • 3
  • 16
  • 36
1

I've authored a script for this purpose. It will fail without printing an error if attempting to override an existing default shortcut.


#!/bin/bash

# Set a Keyboard Shorcut in Gnome
# Example invocation:
#   add_keyboard_shortcut "MyShortcut 0" "gedit" "F8"

has_gsettings=$(which gsettings)
if [[ ! -z "$has_gsettings" ]]; then
  function add_keyboard_shortcut ()
  {
    existing_shortcut_string=$(gsettings get org.gnome.settings-daemon.plugins.media-keys custom-keybindings)
    exst_str_len=$((${#existing_shortcut_string}))
    if (( $exst_str_len  %s' "$1" "$2"
      # Assign bindings
      gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom$new_shortcut_index/ name "$1"
      gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom$new_shortcut_index/ command "$2"
      gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom$new_shortcut_index/ binding "$3"
      # Declare the new binding
      gsettings set org.gnome.settings-daemon.plugins.media-keys custom-keybindings "${declaration_string}"
    else
      printf "\n\nYou are missing input arguments.\n\nThis command requires 3 arguments...\n\nSyntax is add_keyboard_shortcut 'Name' 'Command' 'Shorcut'\n\n\nFor example\n\n  add_keyboard_shortcut 'Open Nautilus' 'nautilus' 'F3'\n\n\n\n\n\nCommon Key Abbreviations:\n\n";
      printf 'Super key:                 \nControl key:                or \nAlt key:                   \nShift key:                 \nnumbers:                   1 (just the number)\nSpacebar:                  space\nSlash key:                 slash\nAsterisk key:              asterisk (so it would need `` as well)\nAmpersand key:             ampersand (so it would need  as well)\n\na few numpad keys:\nNumpad divide key (`/`):   KP_Divide\nNumpad multiply (Asterisk):KP_Multiply\nNumpad number key(s):      KP_1\nNumpad `-`:                KP_Subtract\n\n\n\nList all gsettings keys:\n  gsettings list-recursively';
      return -1;
    fi
  }
fi

J.Dubbs
  • 11
  • 2
0

Here a exaple how to create keyboard shortcut to suspend the system by pressing <Super> + s keys:

gsettings set org.gnome.settings-daemon.plugins.media-keys custom-keybindings "['/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/']"
gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/ name 'suspend'
gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/ command 'systemctl suspend'
gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/ binding '<Super>s'
panticz
  • 1,588
  • 13
  • 14