【问题标题】:Programmatically create a keyboard shortcut in python以编程方式在python中创建键盘快捷键
【发布时间】:2017-01-10 16:05:13
【问题描述】:

我正在为脚本创建设置。我需要设置以包含脚本本身的键盘快捷键。基本上,一个简单的方法是使用相当于 Ubuntu 的 bind 命令的 Windows 7。如何在 Python 中做到这一点?

我尝试过的:

我在某处读到创建vcst 文件将允许我进行键绑定,所以我尝试了这个:

def run_setup(self):
    with open(self.file_name, 'a+') as vsct:
        vsct.write("""<KeyBindings>
 <KeyBinding guid="esc_tool" id="c:\users\{}\desktop\esc_tool\main.py"
        key1="8" mod1="CONTROL" mod2="ALT"/>
</KeyBindings>
            """.format(getpass.getuser()))

那没用。

我怎样才能成功地做到这一点?

编辑

上面的内容似乎有点令人困惑,所以我想做的是从名为setup.py 的脚本内部为名为@9​​87654324@ 的脚本创建一个键盘快捷键。所以:

python setup.py 使用 CNTRL-ALT-8 键创建一个键盘快捷键,该快捷键指向名为 main.py 的脚本。因此,当用户按下 CNTRL-ALT-8 时,它会在 Python 解释器中运行 main.py

【问题讨论】:

  • 您能否将其编辑为更具描述性,以便我想出一个对您有帮助的答案。
  • @Harrison 刚刚做了

标签: python windows-7 keyboard-shortcuts


【解决方案1】:

你介意 setup.py 是不是一个 AutoHotkey 脚本,例如 setup.ahk

缺点:如果你还没有安装一个额外的程序 (AutoHotkey)。

优点:脚本很小:

^!8::            ;defines the shortcut as Ctrl+Alt+8 (Ctrl is ^, Alt is !)
     RunWait, python "C:\Path\To\Your\Script\main.py"
Return

注意:这里假设 python 在 PATH 环境变量中;否则使用第 2 行中 python.exe 的完整路径。

【讨论】:

    猜你喜欢
    • 2011-03-15
    • 1970-01-01
    • 2012-10-11
    • 2015-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-07
    • 2011-03-25
    相关资源
    最近更新 更多