通过 gsettings 在 cinnamon 中自定义全局键绑定

通过 gsettings 在 cinnamon 中自定义全局键绑定

我正在尝试为肉桂设置一些键盘快捷键:

gsettings set org.cinnamon.desktop.keybindings custom-list \
    "['custom0', \
    'custom1', \
    'custom2', \
    'custom3', \
    'custom4']"

## browser
gsettings set org.cinnamon.desktop.keybindings.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/ name "browser"
gsettings set org.cinnamon.desktop.keybindings.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/ command "/home/kaiyin/workspace/bash_scripts/focusRun.sh chromium-browser"
gsettings set org.cinnamon.desktop.keybindings.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/ binding "['<Primary><Shift><Alt>b']"

## file manager
gsettings set org.cinnamon.desktop.keybindings.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom1/ name "fileManager"
gsettings set org.cinnamon.desktop.keybindings.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom1/ command "/home/kaiyin/workspace/bash_scripts/focusRun.sh nemo"
gsettings set org.cinnamon.desktop.keybindings.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom1/ binding "<Primary><Shift><Alt>f"
## terminal
gsettings set org.cinnamon.desktop.keybindings.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom2/ name "terminal"
gsettings set org.cinnamon.desktop.keybindings.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom2/ command "/home/kaiyin/workspace/bash_scripts/focusRun.sh gnome-terminal"
gsettings set org.cinnamon.desktop.keybindings.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom2/ binding "<Primary><Shift><Alt>t"
## gmrun
gsettings set org.cinnamon.desktop.keybindings.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom3/ name "runDialog"
gsettings set org.cinnamon.desktop.keybindings.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom3/ command "/home/kaiyin/workspace/bash_scripts/focusRun.sh gmrun"
gsettings set org.cinnamon.desktop.keybindings.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom3/ binding "<Primary><Shift><Alt>g"

但这没有任何效果。我在 ubuntu 14.04 上使用 cinnamon 2.4.5。

我知道这可以通过系统首选项对话框来完成,但我想在命令行上完成(自动化的更好选择)。

答案1

我在 Ubuntu 14.10 下开始工作。这是将创建自定义键绑定“槽”的命令

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

然后设置名称、命令和绑定值

gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/ name "shutter"
gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/ command "shutter -s"
gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/ binding "Print"

gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom1/ name "shutter"
gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom1/ command "shutter -f"
gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom1/ binding "<Ctrl>Print"

如果我将此与您对肉桂的问题进行比较,我猜问题可能是您必须使用完整路径完全限定要添加的每个项目,如下所示:

gsettings set org.cinnamon.desktop.keybindings custom-list \
"['/org/cinnamon/desktop/keybindings/custom0', \
  '/org/cinnamon/desktop/keybindings/custom1', \
  '/org/cinnamon/desktop/keybindings/custom2', \
  '/org/cinnamon/desktop/keybindings/custom3', \
  '/org/cinnamon/desktop/keybindings/custom4']"

答案2

这对我有用(Debian 9.4 上的 Cinnamon 3.2.7):

   gsettings set org.cinnamon.desktop.keybindings custom-list \
        "['custom0', \
        'custom1', \
        'custom2', \
        'custom3', \
        'custom4']"


## browser
gsettings set org.cinnamon.desktop.keybindings.custom-keybinding:/org/cinnamon/desktop/keybindings/custom-keybindings/custom0/ name "browser"
gsettings set org.cinnamon.desktop.keybindings.custom-keybinding:/org/cinnamon/desktop/keybindings/custom-keybindings/custom0/ command "/home/kaiyin/workspace/bash_scripts/focusRun.sh chromium-browser"
gsettings set org.cinnamon.desktop.keybindings.custom-keybinding:/org/cinnamon/desktop/keybindings/custom-keybindings/custom0/ binding "['<Primary><Shift><Alt>b']"

## file manager
gsettings set org.cinnamon.desktop.keybindings.custom-keybinding:/org/cinnamon/desktop/keybindings/custom-keybindings/custom1/ name "fileManager"
gsettings set org.cinnamon.desktop.keybindings.custom-keybinding:/org/cinnamon/desktop/keybindings/custom-keybindings/custom1/ command "/home/kaiyin/workspace/bash_scripts/focusRun.sh nemo"
gsettings set org.cinnamon.desktop.keybindings.custom-keybinding:/org/cinnamon/desktop/keybindings/custom-keybindings/custom1/ binding "['<Primary><Shift><Alt>f']"
## terminal
gsettings set org.cinnamon.desktop.keybindings.custom-keybinding:/org/cinnamon/desktop/keybindings/custom-keybindings/custom2/ name "terminal"
gsettings set org.cinnamon.desktop.keybindings.custom-keybinding:/org/cinnamon/desktop/keybindings/custom-keybindings/custom2/ command "/home/kaiyin/workspace/bash_scripts/focusRun.sh gnome-terminal"
gsettings set org.cinnamon.desktop.keybindings.custom-keybinding:/org/cinnamon/desktop/keybindings/custom-keybindings/custom2/ binding "['<Primary><Shift><Alt>t']"
## gmrun
gsettings set org.cinnamon.desktop.keybindings.custom-keybinding:/org/cinnamon/desktop/keybindings/custom-keybindings/custom3/ name "runDialog"
gsettings set org.cinnamon.desktop.keybindings.custom-keybinding:/org/cinnamon/desktop/keybindings/custom-keybindings/custom3/ command "/home/kaiyin/workspace/bash_scripts/focusRun.sh gmrun"
gsettings set org.cinnamon.desktop.keybindings.custom-keybinding:/org/cinnamon/desktop/keybindings/custom-keybindings/custom3/ binding "['<Primary><Shift><Alt>g']"

通过运行以下命令验证您的自定义键绑定是否已设置:

dconf dump /org/cinnamon/

新的绑定也可以在设置 GUI 中看到

答案3

我观察到使用“gsettings”设置快捷方式(在我的例子中是/org/cinnamon/desktop/keybindings/custom-keybindings)并没有(直接)效果。只有当我在 GUI 中进行一些(甚至不相关的)更改时,快捷方式才会真正刷新。

我编写了一个Python程序(基于cinnamon GUI的代码),它可以切换快捷键脚本(F21和Multi_key之间)并具有直接效果。我想将其更改为您的特定用例应该不会太难。

#!/usr/bin/env python2
import subprocess
from gi.repository import Notify
from gi.repository import Gio, Gdk

CUSTOM_KEYS_PARENT_SCHEMA = "org.cinnamon.desktop.keybindings"
CUSTOM_KEYS_BASENAME = "/org/cinnamon/desktop/keybindings/custom-keybindings"
CUSTOM_KEYS_SCHEMA = "org.cinnamon.desktop.keybindings.custom-keybinding"

curr_shortcut = subprocess.check_output(['gsettings','get', 'org.cinnamon.desktop.keybindings.custom-keybinding:/org/cinnamon/desktop/keybindings/custom-keybindings/custom0/', 'binding'])

custom_num=0

def remove_custom():
    keybinding_path='custom0'
    custom_path = CUSTOM_KEYS_BASENAME + "/" + keybinding_path + "/"
    custom_schema = Gio.Settings.new_with_path(CUSTOM_KEYS_SCHEMA, custom_path)
    custom_schema.delay()
    custom_schema.reset("name")
    custom_schema.reset("command")
    custom_schema.reset("binding")
    custom_schema.apply()
    Gio.Settings.sync()

    parent_settings = Gio.Settings(CUSTOM_KEYS_PARENT_SCHEMA)
    array = parent_settings.get_strv("custom-list")

    existing = False
    for entry in array:
        if keybinding_path == entry:
            existing = True
            break
    if existing:
        array.remove(keybinding_path)
        parent_settings.set_strv("custom-list", array)

def add_custom(key):
    parent = Gio.Settings.new(CUSTOM_KEYS_PARENT_SCHEMA)
    array = parent.get_strv("custom-list")

    new_str = "custom" + str(custom_num)
    array.append(new_str)
    parent.set_strv("custom-list", array)
    new_path = CUSTOM_KEYS_BASENAME + "/custom" + str(custom_num) + "/"
    new_schema = Gio.Settings.new_with_path(CUSTOM_KEYS_SCHEMA, new_path)
    new_schema.delay()
    new_schema.set_string("name", 'command_name')
    new_schema.set_string("command",'/path/to/script')
    new_schema.set_strv("binding",[key])
    new_schema.apply()
    Gio.Settings.sync()


if 'Multi_key' in curr_shortcut:
    remove_custom()
    add_custom('F21')
    Notify.init ("Shortcut disabled")
    SC_ON=Notify.Notification.new ("Shortcut","Shortcut disabled","dialog-information")
    SC_ON.set_urgency(0)
    SC_ON.show()
else:
    remove_custom()
    add_custom('Multi_key')
    Notify.init ("Shortcut enabled")
    SC_OFF=Notify.Notification.new ("Shortcut","Shortcut enabled","dialog-information")
    SC_OFF.set_urgency(0)
    SC_OFF.show()

答案4

为 Cinnamon 20.2 执行此操作的一行:

gsettings set org.cinnamon.desktop.interface gtk-key-theme 'Emacs'

相关内容