5

Is there a way of using dconf load / < [...] for keyboard shortcuts but without mandatory re-logging of User? And/or possibly without admin rights?

I'm using input file like this:

[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/custom0]
binding='<Super>c'
command='gnome-calculator'
name='Calculator'

[org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom1]
binding='<Super>z'
command='gnome-system-monitor'
name='System Monitor'

These settings won't work until new session starts.

madneon
  • 1,354
  • 1
  • 11
  • 19
  • 1
    The changes [take place immediately](http://unix.stackexchange.com/q/107402) so there's no need to log out and log back in (not sure where you got the "mandatory" part from...) If that's not how it works on your system then something's not right on your side. – don_crissti Nov 14 '16 at 13:51
  • 1
    @don_crissti if so, any idea how to diagnose whats wrong? – madneon Nov 16 '16 at 19:18
  • Create a new user account. If it works then you know it's your user account. If it doesn't maybe it's time to change the distro... – don_crissti Nov 16 '16 at 19:57

3 Answers3

5

You can use GSettings

Simple script:

#!/bin/bash

gsettings set 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/']"

# Keybind 0
gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/ binding "<Super>c"
gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/ command "gnome-calculator"
gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/ name "Calculator"

# Keybind 1
gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom1/ binding "<Super>z"
gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom1/ command "gnome-system-monitor"
gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom1/ name "System Monitor"
Rejedai
  • 51
  • 1
  • 3
0

I've authored a custom script for this purpose.

Usage:

add_keyboard_shortcut "MyShortcut#1" "gedit" "&lt;Control&gt;F8"

And here is the script:

#!/bin/bash
    
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 < 9 )); then
      existing_shortcut_count=0
    else
      IFS=', ' read -ra existing_shortcut_array <<< "$existing_shortcut_string"
      existing_shortcut_count="${#existing_shortcut_array[@]}"
    fi
    new_shortcut_index=$(("$existing_shortcut_count"))
    declaration_string=' ['
    for (( i=0; i<="$existing_shortcut_count"; i++ )); do
      if (( $i == 0 ))
      then
        declaration_string="$declaration_string""'/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom$i/'"
      else
        declaration_string="$declaration_string"", '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom$i/'"
      fi
    done
    declaration_string="$declaration_string"']'
    if [[ ! -z "$1" ]] && [[ ! -z "$2" ]] && [[ ! -z "$3" ]]; then
      printf "\nFound %s existing custom shortcuts.\n\n" "$existing_shortcut_count"
      printf 'Setting new custom keyboard shortcut "%s" => %s' "$1" "$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/ 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"
      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' '<Control>F3'\n\n\n\n\n\nCommon Key Abbreviations:\n\n";
      printf 'Super key:                 <Super>\nControl key:               <Primary> or <Control>\nAlt key:                   <Alt>\nShift key:                 <Shift>\nnumbers:                   1 (just the number)\nSpacebar:                  space\nSlash key:                 slash\nAsterisk key:              asterisk (so it would need `<Shift>` as well)\nAmpersand key:             ampersand (so it would need <Shift> 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
AdminBee
  • 21,637
  • 21
  • 47
  • 71
  • little nit here. when running bash you should use "type" instead of "which". https://unix.stackexchange.com/questions/85249/why-not-use-which-what-to-use-then?noredirect=1&lq=1 – kdubs Sep 06 '22 at 14:54
-1

You can also restart your gnome-settings-daemon. New parameters will be taken in account after restart.

madneon
  • 1,354
  • 1
  • 11
  • 19
pierre
  • 1