如同这个问题来自 2012 年,我正在寻找一种方法来自动设置组合键,该组合键应该以与在 GUI 中设置相同的方式触发命令的执行。(示例用法)
我在 Ubuntu 18.04 上,链接答案的 gsettings 模式似乎不存在:
$ gsettings list-schemas | grep keyboard
org.gnome.settings-daemon.plugins.keyboard
org.gnome.settings-daemon.plugins.a11y-keyboard
org.gnome.shell.keyboard
org.gnome.settings-daemon.peripherals.keyboard
org.gnome.libgnomekbd.keyboard
com.canonical.indicator.keyboard
org.gnome.desktop.peripherals.keyboard
org.gnome.desktop.a11y.keyboard
我如何使用 bash 进行设置,就像我进入 GNOME GUISettings > Devices > Keyboard
并手动进行设置一样?
答案1
评论后
不太容易。
您需要在org.gnome.settings-daemon.plugins.media-keys
[called]中为快捷键声明一个文件夹custom-keybindings
,然后在 下创建该文件夹/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/
。
在该文件夹中,键“binding”、“command”和“name”定义自定义快捷键。不确定如何使用命令轻松完成此操作(尤其是第一步)。–钒
我查看了以下链接:
- 如何向空架构添加键在 Linux Mint 上
- Gsettings 架构类型
- 创建架构
- gsettings 的 set 命令中的数组操作
我的笔记本电脑上已经使用 GUI 进行了设置,我运行了一些查询来了解我应该瞄准的输出。
$ gsettings list-relocatable-schemas | grep keybin
org.gnome.settings-daemon.plugins.media-keys.custom-keybinding
org.gnome.orca.keybinding
## I have two shortcuts
$ gsettings get 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 get org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom1/ binding
'<Primary><Super>Left'
$ ls /opt
confinedrv virtio-win.iso workspace_shortcut_send_to_next.sh
generic@motorbrot:~/Downloads/iso.new$ gsettings get org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom1/ command
'wmctrl -r :ACTIVE: -t 0'
$ gsettings get org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom1/ name
'send to first workspace'
在我的测试设备上(不同于之前的笔记本电脑),查询org.gnome.settings-daemon.plugins.media-keys custom-keybindings
返回一个空字符串数组(@as []
)。
在我的测试设备上,我尝试设置绑定:
$ gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom1/ binding '<Primary><Super>Right'
No such schema "org.gnome.settings-daemon.plugins.media-keys.custom-keybindings"
'<Primary><Super>Left'
在/usr/share/glib-2.0/schemas/org.gnome.settings-daemon.plugins.media-keys.custom-keybindings.gschema.xml
测试设备上有一个为 定义的模式(没有尾随的 s)。在那里我可以看到、和org.gnome.settings-daemon.plugins.media-keys.custom-keybinding
的类型都是字符串。所以我删除了 中的尾随 s但没有删除,现在再次设置和查询它们就可以了。name
binding
command
s
id
path
$ gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/ binding '<Primary><Super>Right'
$ gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/ name 'move to first wirkspace'
$ gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/ command 'wmctrl -r :ACTIVE: -t 0'
$ gsettings get org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/ binding
'<Primary><Super>Right'
然后,我将自定义命令路径添加到之前为空的列表中。(我认为这会覆盖整个列表,所以要小心!)
gsettings set org.gnome.settings-daemon.plugins.media-keys custom-keybindings "['/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/']"
## make sure the path starts and ends with a slash
gsettings get org.gnome.settings-daemon.plugins.media-keys custom-keybindings
"['/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/']"
我通过r
按下 后出现的弹出窗口进入重新加载 gnome 的过程AltF2。
我将窗口移至第二个工作区并尝试它是否有效。它不起作用 - 什么都没发生。
我将命令更改为echo test > ~/testing.txt
并再次尝试,但仍然 - 什么都没发生。文件未创建。
这里的问题与我在 GUI 中使用的链接示例相同:
如果快捷方式无法正常工作,但从终端运行时命令可以正常工作,请将其保存在文件中
/fullpath/myfile.txt
并设置bash /fullpath/myfile.txt
为快捷方式命令。这应该在 Gnome 和 Xfce 上都有效。
答案2
我为此编写了一个脚本。如果尝试覆盖现有的默认快捷方式,它将失败,且不会打印错误。
#!/bin/bash
# Set a Keyboard Shorcut in Gnome
# Example invocation:
# add_keyboard_shortcut "MyShortcut 0" "gedit" "F8"
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 %s' "$1" "$2"
# Assign bindings
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"
# Declare the new binding
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' 'F3'\n\n\n\n\n\nCommon Key Abbreviations:\n\n";
printf 'Super key: \nControl key: or \nAlt key: \nShift key: \nnumbers: 1 (just the number)\nSpacebar: space\nSlash key: slash\nAsterisk key: asterisk (so it would need `` as well)\nAmpersand key: ampersand (so it would need 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
答案3
这里有一个如何创建键盘快捷键来通过按键来挂起系统的示例<Super> + s
:
gsettings set org.gnome.settings-daemon.plugins.media-keys custom-keybindings "['/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/']"
gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/ name 'suspend'
gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/ command 'systemctl suspend'
gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/ binding '<Super>s'