72 votes

Comment définir des raccourcis clavier personnalisés à partir du terminal ?

Comment définir des raccourcis clavier personnalisés à partir du terminal pour différentes versions de Linux ?

En gros, je veux savoir où Linux stocke les fichiers de raccourcis clavier et comment ils peuvent être modifiés.

Lors de mes recherches, j'ai trouvé un fichier ~/.config/compiz-1/compizconfig mais il était plus ou moins crypté quand j'ai essayé de l'ouvrir avec nano .

1voto

mmai Points 1214

Vous pouvez définir un nouveau raccourci personnalisé sans script Python, en utilisant sed. Il vous suffit de définir nom , liaison y action à votre choix dans le script suivant :

name="myaction"
binding="<CTRL><ALT>v"
action="/usr/local/bin/myaction"

media_keys=org.gnome.settings-daemon.plugins.media-keys
custom_kbd=org.gnome.settings-daemon.plugins.media-keys.custom-keybinding
kbd_path=/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/$name/
new_bindings=`gsettings get $media_keys custom-keybindings | sed -e"s>'\]>','$kbd_path']>"| sed -e"s>@as \[\]>['$kbd_path']>"`
gsettings set $media_keys custom-keybindings "$new_bindings"
gsettings set $custom_kbd:$kbd_path name $name
gsettings set $custom_kbd:$kbd_path binding $binding
gsettings set $custom_kbd:$kbd_path command $action

1voto

AlikElzin-kilaka Points 4588

J'ai écrit un script pour ça. Voir ci-dessous.

Voir l'utilisation dans le creatShortcut l'invocation.

export nextShortcutId=0
function creatShortcut() {
    name="$1"
    commandToRun="$2"
    binding="$3"
    path="/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom${nextShortcutId}"
    nextShortcutId=$nextShortcutId+1
    dconf write "$path/name" "'""$name""'"
    dconf write "$path/command" "'""$commandToRun""'"
    dconf write "$path/binding" "'""$binding""'"
}

# dconf write /org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/binding '"<Primary><Shift>exclam"'
creatShortcut 'copyq show' 'copyq show' '<Primary><Shift>exclam'
creatShortcut 'System Monitor' 'gnome-system-monitor' '<Primary><Alt>m'
creatShortcut 'Suspend' 'systemctl suspend -i' '<Super>d'
creatShortcut 'Volume Up' 'amixer -D pulse sset Master 5%+' '<Super>Page_Up'
creatShortcut 'Volume Down' 'amixer -D pulse sset Master 5%-' '<Super>Page_Down'

overallbindings=""
for ((i = 0 ; i < $nextShortcutId ; i++ ));
do
    overallbindings="$overallbindings, '$customindingPathPrefix$i/'"
done
overallbindings="[${overallbindings:2}]" # Delete the first 2 chars: " ," - space and comma
# echo $overallbindings

# Update the list of bindings for the shortcuts to work
dconf write /org/gnome/settings-daemon/plugins/media-keys/custom-keybindings "$overallbindings"
# dconf write /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/custom2/', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom3/', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom4/', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom5/']"

1voto

SauloAlessandre Points 111

J'ai renoncé à utiliser dconf et tweak tool, j'utilise kbindkeys avec ~/.xbindkeysrc et mets tous mes raccourcis clavier personnalisés, c'est beaucoup plus portable.

  1. Installez d'abord xbindkeys $ sudo apt install xbindkeys

  2. Générez les .xbindkeysrc par défaut la première fois seulement, la fois suivante, copiez-les chez vous. $ xbindkeys --defaults > ~/.xbindkeysrc

  3. Mettez vos raccourcis clavier dans .xbindkeysrc, par exemple, utilisez Ctrl+Shift+F pour passer à la musique suivante sur Spotify :

    "dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Next"
          m:0x19 + c:41
  4. Autre ex appeler gnome-calculator avec Ctrl+Alt+Super+c mettre la ligne ci-dessus dans .xbindkeysrc :

          Control+Alt+Mod2+Mod4 + c

Le format de xbindkeys est simple, la première ligne est la commande et la deuxième ligne est la touche courte.

Pour découvrir la touche courte, utilisez : $ xbindkeys -k et appuyez sur les touches.

N'oubliez pas de redémarrer xbindkeys lorsque vous ajoutez de nouveaux shortkeys.

0voto

tukusejssirs Points 141

J'ai trouvé le réponse posté par @JacobVlijm très utile, surtout le script. J'ai porté le code vers bash . Je ne pense pas que cette fonction soit parfaite, elle peut contenir quelques bogues, cependant, elle fonctionne pour moi.

Ne t'attends pas à ce que je mette à jour ce script ici. Vous pouvez trouver la dernière version du script (et toutes ses révisions) aquí .

function set_shortcuts(){
    # Usage: set_shortcuts [name] [command] [shortcut]
    unset num i name command shortcut value test value_new
    local name="$1"
    local command="$2"
    local shortcut="$3"
    local value=$(gsettings get org.gnome.settings-daemon.plugins.media-keys custom-keybindings)
    local test=$(echo $value | sed "s/\['//;s/', '/,/g;s/'\]//" - | tr ',' '\n' | grep -oP ".*/custom\K[0-9]*(?=/$)")

    if [ "$(echo "$value" | grep -o "@as")" = "@as" ]; then
        local num=0
        local value_new="['/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom${num}/']"
    else
        local i=1
        until [ "$num" != "" ]; do
            if [ "$(echo $test | grep -o $i)" != "$i" ]; then
                local num=$i
            fi
            i=$(echo 1+$i | bc);
        done
        local value_new=$(gsettings get org.gnome.settings-daemon.plugins.media-keys custom-keybindings | sed "s#']\$#', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom${num}/']#" -)
    fi

    gsettings set org.gnome.settings-daemon.plugins.media-keys custom-keybindings "$value_new"
    gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom${num}/ name "$name"
    gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom${num}/ command "$command"
    gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom${num}/ binding "$shortcut"
}

0voto

user2804197 Points 101

Voici un script Python qui utilise les liaisons PyGObject au lieu d'appeler gsettings et d'analyser la sortie. Lorsqu'un raccourci clavier du même nom existe déjà, il le remplace au lieu d'ajouter un doublon.

#!/usr/bin/env python3

import sys

from gi.repository import Gio, GLib

media_keys_schema = "org.gnome.settings-daemon.plugins.media-keys"
custom_keybinding_schema = "org.gnome.settings-daemon.plugins.media-keys.custom-keybinding"
keybinding_path_prefix = "/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/"

usage_message = '''Usage: {cmd} <name> <command> <binding>

Example:
{cmd} "Open gedit" "gedit" "<Shift><Ctrl>G"
'''.format(cmd=sys.argv[0])

def main():
    if len(sys.argv) < 4:
        sys.exit(usage_message)
    name = sys.argv[1]
    command = sys.argv[2]
    binding = sys.argv[3]

    settings = Gio.Settings(schema=media_keys_schema)
    binding_paths = settings.get_value("custom-keybindings").unpack()  # type: list[str]

    # Figure out which path to use for the new binding. If a binding
    # of the same name already exists, we replace it.
    path = find(binding_paths, name)
    if not path:
        # Use the first gsettings path that doesn't exist yet
        n = 0
        path = keybinding_path_prefix + f"custom{n}/"
        while path in binding_paths:
            n += 1
            path = keybinding_path_prefix + f"custom{n}/"

    # Create the new keybinding
    setting = Gio.Settings(schema=custom_keybinding_schema, path=path)
    setting.set_string("name", name)
    setting.set_string("command", command)
    setting.set_string("binding", binding)
    setting.sync()

    if path not in binding_paths:
        # Add the new keybinding to the list of custom keybindings
        binding_paths.append(path)
        settings.set_value("custom-keybindings", GLib.Variant("as", binding_paths))
        setting.sync()

def find(binding_paths: list[str], name: str) -> str:
    for path in binding_paths:
        setting = Gio.Settings(schema=custom_keybinding_schema, path=path)
        setting_name = setting.get_value("name").unpack()
        if setting_name == name:
            return path

if __name__ == "__main__":
    main()

SistemesEz.com

SystemesEZ est une communauté de sysadmins où vous pouvez résoudre vos problèmes et vos doutes. Vous pouvez consulter les questions des autres sysadmins, poser vos propres questions ou résoudre celles des autres.

Powered by:

X