【发布时间】: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