使用 gsettings 在 Ubuntu 22.04 中设置键盘快捷键(有什么变化?)

使用 gsettings 在 Ubuntu 22.04 中设置键盘快捷键(有什么变化?)

Super+C看起来使用 gsettings 和命令行在 Ubuntu 22.04 中设置启动我的 GNOME 计算器应该相对容易:

gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom999/ name "'Launch Calculator'"
gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom999/ binding "'<Super>c'"
gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom999/ command "'gnome-calculator'"

我发出命令,一切似乎都正常,然后我按下Super+C,什么也没有发生。我做错了什么?

这将是脚本的一部分,所以我不能使用任何 GUI 解决方案,尽管我们可以使用 GUI(dconf、设置等)进行故障排除。事实上,这就是我所做的,我可以看到我的

dconf > org > gnome > settings-daemon > plugins > media-keys > custom-keybindings

然而,我的

Settings > Keyboard > Keyboard Shortcuts > View and Customize Shortcuts

这似乎解释了为什么我按下 时“什么都没有发生” Super+C

答案1

每个自定义目录 (结尾为/)在目录下添加

/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/

这是以以下结尾的目录/)需要将其路径作为值条目添加到钥匙

/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings

这是一个与上面提到的目录同名的键,但是不是结尾为/
...你可以用以下方式读取其当前值

dconf read /org/gnome/settings-daemon/plugins/media-keys/custom-keybindings

所以,此外创建自定义目录并用键和值填充它,如下所示(使用dconf):

dconf write /org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom999/binding "'<Super>c'"
dconf write /org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom999/command "'gnome-calculator'"
dconf write /org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom999/name "'Launch Calculator'"

...你可以dconf dump这样验证:

$ dconf dump /org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom999/
[/]
binding='<Super>c'
command='gnome-calculator'
name='Launch Calculator'

...你需要将其路径添加到/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings键,同时保留该键数组中预先存在的值,['...', '...', ...]如下所示:

if [ -z "$(dconf read /org/gnome/settings-daemon/plugins/media-keys/custom-keybindings | tr -d '][')" ]; then
  dconf write /org/gnome/settings-daemon/plugins/media-keys/custom-keybindings "['/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom999/']"
  else
  dconf write /org/gnome/settings-daemon/plugins/media-keys/custom-keybindings "[$(dconf read /org/gnome/settings-daemon/plugins/media-keys/custom-keybindings | tr -d '][') , '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom999/']"
fi

...你可以dconf read这样验证:

$ dconf read /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/custom999/']

注意我使用该dconf工具是因为它在所有 Ubuntu 安装中默认可用,因此为广泛部署的脚本提供了开箱即用的支持,但您可以安装gsettings前端工具并使用它来修改上述命令的语法和目录/关键路径样式以使用它...参见man gsettingsgsettings list-schemasgsettings list-recursively在使用之前查看其输出。

相关内容