How to set a custom keyboard shortcut from a bash script with GNOME3 in Ubuntu 18.04?
After the comment
Not quite easy.
You need to declare a folder for the shortcut key inorg.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:
- How to Add a Key to a Empty Schema on Linux Mint
- Gsettings Schema Types
- Creating A Schema
- Array manipulation in gsettings' set command
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 setbash /fullpath/myfile.txt
as the shortcut command. That should work both on Gnome and Xfce.