【问题标题】:pynput custom autokey script isn't working when following video guide遵循视频指南时,pynput 自定义自动键脚本不起作用
【发布时间】:2021-07-03 09:53:43
【问题描述】:

我想创建一个热键,但决定使用 Python 而不是 AHK 来“挑战”自己。

我希望在按下组合键 Ctrl+Alt+P(或 p)时打印出短语 Scheiße!

我参考了this的视频,写了如下脚本:

from pynput import keyboard

COMBINATIONS = [
{keyboard.Key.ctrl, keyboard.Key.alt, keyboard.KeyCode(char='p')}, # Detects ctrl+alt+p
{keyboard.Key.ctrl, keyboard.Key.alt, keyboard.KeyCode(char='P')} # Detects ctrl+alt+P
]

current = set()

def execute():
 print ("Scheiße!") # When the above combo is pressed, the word "Scheiße!" is printed

def on_press(key):
    if any([key in COMBO for COMBO in COMBINATIONS]): # Checks if pressed key is in any combinations
        current.add(key) # If it is, then it's added to the current key set
        if any(all (k in current for k in COMBO) for COMBO in COMBINATIONS): # Checks if every key of the combination has been pressed
                execute() # If all checks pass, execute is called
def on_release(key):
    if any([key in COMBO for COMBO in COMBINATIONS]): # Checks if a key released is in any of the combinations
        current.remove(key) # If it is, then it's remove from the current key set if applicable

with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:
    listener.join()

当我执行脚本时,出现以下错误:

    ================== RESTART: C:\Users\IMSOASIAN\Desktop\Scheiße!.py =================
Unhandled exception in listener callback
Traceback (most recent call last):
  File "C:\Users\IMSOASIAN\AppData\Local\Programs\Python\Python39\lib\site-packages\pynput\_util\__init__.py", line 211, in inner
    return f(self, *args, **kwargs)
  File "C:\Users\IMSOASIAN\AppData\Local\Programs\Python\Python39\lib\site-packages\pynput\keyboard\_win32.py", line 287, in _process
    self.on_release(key)
  File "C:\Users\IMSOASIAN\AppData\Local\Programs\Python\Python39\lib\site-packages\pynput\_util\__init__.py", line 127, in inner
    if f(*args) is False:
  File "C:\Users\IMSOASIAN\Desktop\Scheiße!.py", line 20, in on_release
    current.remove(key) # If it is, then it's remove from the current key set if applicable
KeyError: 'p'
Traceback (most recent call last):
  File "C:\Users\IMSOASIAN\Desktop\Scheiße!.py", line 23, in <module>
    listener.join()
  File "C:\Users\IMSOASIAN\AppData\Local\Programs\Python\Python39\lib\site-packages\pynput\_util\__init__.py", line 259, in join
    six.reraise(exc_type, exc_value, exc_traceback)
  File "C:\Users\IMSOASIAN\AppData\Local\Programs\Python\Python39\lib\site-packages\six.py", line 702, in reraise
    raise value.with_traceback(tb)
  File "C:\Users\IMSOASIAN\AppData\Local\Programs\Python\Python39\lib\site-packages\pynput\_util\__init__.py", line 211, in inner
    return f(self, *args, **kwargs)
  File "C:\Users\IMSOASIAN\AppData\Local\Programs\Python\Python39\lib\site-packages\pynput\keyboard\_win32.py", line 287, in _process
    self.on_release(key)
  File "C:\Users\IMSOASIAN\AppData\Local\Programs\Python\Python39\lib\site-packages\pynput\_util\__init__.py", line 127, in inner
    if f(*args) is False:
  File "C:\Users\IMSOASIAN\Desktop\Scheiße!.py", line 20, in on_release
    current.remove(key) # If it is, then it's remove from the current key set if applicable
KeyError: 'p'

我尝试了各种故障排除步骤,例如删除德语字符、将“p”和“alt”替换为其他键,但唯一能做的就是更改输出。

有人对此有解决方案吗?

【问题讨论】:

    标签: python visual-studio-code pynput


    【解决方案1】:

    根据你提供的信息,我在VS Code中测试了视频中的代码,可以在VS Code中运行:

    请使用代码:

    COMBINATIONS = [
        {keyboard.Key.shift, keyboard.KeyCode(char='p')}, # Detects ctrl+alt+p
        {keyboard.Key.shift, keyboard.KeyCode(char='P')} # Detects ctrl+alt+P
    ]
    

    另外,请重新加载 VS Code 使其能够识别已安装的模块,并尝试在 VS Code 中运行几次。

    【讨论】:

    • 你知道如何从命令行激活热键吗?
    • @IMSOASIAN - 如何理解“命令行外”?
    • 就像在任何应用程序中工作,而不是在我启动它的应用程序中工作。它只适用于我启动它的终端。
    • 是的,但它会在 INSIDE vs code 中输出。我希望它在任何应用程序的任何文本字段中输出。
    • @IMSOASIAN - 修改代码后,我在VS Code中点击了运行按钮,然后在VS Code的内部终端中点击“Shift”并输入“p”。结果在这个终端输出,然后我打开系统'cmd'终端点击“Shift”输入“p”,结果在VS Code终端输出。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-04
    • 1970-01-01
    • 2018-06-01
    相关资源
    最近更新 更多