这个问题与这个问题类似: 键盘快捷键用于打开 Ubuntu 文件管理器?
除此之外,我想知道是否可以通过命令行而不是 GUI 来完成此操作。
任务是在新安装的 Ubuntu 上设置/home/usr
的快捷方式。super+E
这样做的原因是,通过 GUI 配置多台机器很麻烦。作为最后的手段,总是可以模拟鼠标和键盘来实现自动化。但是,对于这个问题,假设这不是一个选择。
答案1
代码类似这样:如何从终端设置自定义键盘快捷键? 除了修复了没有自定义键时的错误。也许有权限的人可以混淆答案,然后删除这个。
Python部分:
#!/usr/bin/env python3
import subprocess
import sys
# defining keys & strings to be used
key = "org.gnome.settings-daemon.plugins.media-keys custom-keybindings"
subkey1 = key.replace(" ", ".")[:-1]+":"
item_s = "/"+key.replace(" ", "/").replace(".", "/")+"/"
firstname = "custom"
# get the current list of custom shortcuts
get = lambda cmd: subprocess.check_output(["/bin/bash", "-c", cmd]).decode("utf-8")
# No custom keys
if get("gsettings get "+key) == "@as []\n":
n = 0
current = list()
new = item_s+firstname+str(n)+"/"
# Found custom keys
else:
current = eval(get("gsettings get "+key))
# make sure the additional keybinding mention is no duplicate
n = 1
while True:
new = item_s+firstname+str(n)+"/"
if new in current:
n = n+1
else:
break
# add the new keybinding to the list
current.append(new)
# create the shortcut, set the name, command and shortcut key
cmd0 = 'gsettings set '+key+' "'+str(current)+'"'
cmd1 = 'gsettings set '+subkey1+new+" name '"+sys.argv[1]+"'"
cmd2 = 'gsettings set '+subkey1+new+" command '"+sys.argv[2]+"'"
cmd3 = 'gsettings set '+subkey1+new+" binding '"+sys.argv[3]+"'"
for cmd in [cmd0, cmd1, cmd2, cmd3]:
subprocess.call(["/bin/bash", "-c", cmd])
在命令行中调用上面的脚本:
python3 /path/to/script.py 'home folder' 'nautilus /home/user' '<Super>e'