2

i can retrieve the list of custom key bindings by:

gsettings get org.gnome.settings-daemon.plugins.media-keys custom-keybindings

which return something like:

['/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/custom2/']

But i want to know target key binding via its name? i.e. shutter -> <primary><shift><alt>a

I've figured out one simple but not convenient approach:

gsettings get org.gnome.settings-daemon.plugins.media-keys.custom-  
keybinding:/org/gnome/settin‌​gs-daemon/plugins/media-keys/custom-  
keybindings/custom0/ name
// return the name like `shutter`


gsettings get org.gnome.settings-daemon.plugins.media-keys.custom-  
keybinding:/org/gnome/settin‌​gs-daemon/plugins/media-keys/custom-  
keybindings/custom0/ binding
// return the binding like `<primary><shift><alt>a`

Does any guy know other elegant solutions?

Hizqeel
  • 1,895
e-cloud
  • 241

2 Answers2

4

Small script to find the keybinding by name

The script below will output the keybinding when you run it with the shortcut's name as argument. an example:

$ python3 '/home/jacob/Bureaublad/find_keybinding.py' rename
> '<Primary><Alt>r'

How to set up

  • Copy the script below into an empty file, save it as find_keybinding.py
  • Run it by the command:

    python3 '/path/to/find_keybinding.py' <shortcut_name>
    

Explanation

The information is in the output of

dconf dump /

On the custom keyboard shortcuts, this will output sections like:

[org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom9]
binding='<Primary><Alt>r'
command='/home/jacob/.local/share/nautilus/scripts/change_name'
name='rename'

As you can see, we need the line, two lines above

name='rename'

...and so the script outputs that line, stripped from binding=

The script

#!/usr/bin/env python3
import subprocess; import sys
key = "/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/"
# read the output from dconf dump /, split into lines
ls = [l for l in subprocess.check_output(["dconf", "dump", key]).decode("utf-8").splitlines()]
# find line corresponding to searched name, print two lines higher
print(ls[ls.index([l for l in ls if "name='"+sys.argv[1] in l][0])-2].replace("binding=", ""))

Additionally

...you could create the directory ~/bin (if it doesn't exist yet) and save the script there without extension, and make it executable. Log out and back in, then simply:

find_keybinding <name> 

will do

Jacob Vlijm
  • 83,767
  • Thank you anyway. That inspires me to implement the same thing through shell. – e-cloud Jul 01 '16 at 08:31
  • @SaintScott I got a (very long) oneliner, but the python option is more robust. Not a single exception on the weirdest names, name clashes etc. – Jacob Vlijm Jul 01 '16 at 08:34
  • sorry, i mean, through shell script, actually doing the similar thing like yours – e-cloud Jul 01 '16 at 10:32
  • @SaintScott I understand, that's what I did as well, I actually have one, but doing it in python turns out to be more straightforward and robust. Also, the question mentions no language preference :) – Jacob Vlijm Jul 01 '16 at 10:34
  • i'm not rejecting your answer, but just comment. :) – e-cloud Jul 01 '16 at 10:44
  • @SaintScott Not taken as such! I was just commenting on your comment :) – Jacob Vlijm Jul 01 '16 at 10:45
  • 1
    If @JacobVlijm's answer helped you, please take a moment to accept it. Thank you! – grooveplex Jul 04 '16 at 07:26
  • @SaintScott ^ just curious, but is there a specific reason why you wouldn't accept it? Both the Q & A were kind of on your request, in the comments of this answer: http://askubuntu.com/a/597414/72216 – Jacob Vlijm Jul 04 '16 at 07:34
  • sorry, i'm a newbie to ask a question that i didn't realized that i should accept the answer. :( – e-cloud Jul 05 '16 at 08:26
  • @SaintScott thanks! Only if it solves your issue in a satisfying way :) – Jacob Vlijm Jul 05 '16 at 08:34
  • I have tried executing this script with a number of inputs, but keep getting the same result:

    IndexError: list index out of range.

    I am assuming no such binding exists. If so, can somebody harden the script?

    – kblmfld May 11 '18 at 18:11
0

For custom keys try:

for i in $(gsettings get org.gnome.settings-daemon.plugins.media-keys custom-keybindings | awk -F"'" '{ for (i=2;i<=NF;i+=2) print $i }'); do
    echo "$(dconf read ${i}binding) $(dconf read ${i}name)"
done

For other keys, grep over the following command:

for schema in $(gsettings list-schemas | grep -E 'keybindings|media-keys')
do
    gsettings list-recursively $schema
done

Check this for more.