我是python和编写热键脚本的新手。我正在编写一个热键脚本来帮助自动化部分工作。我已经编写了脚本,并且似乎不需要长if语句就可以工作。现在它位于if语句中,它会引发错误。错误是侦听器回调中的未处理异常。是什么导致此错误?
脚本是:
from pynput import keyboard
import datetime
import AdvancedHTMLParser
# The key combinations to check
COMBINATIONS = [
{keyboard.Key.ctrl_l, keyboard.Key.shift, keyboard.KeyCode(char='c')},
{keyboard.Key.ctrl_r, keyboard.Key.shift, keyboard.KeyCode(char='c')},
{keyboard.Key.ctrl_l, keyboard.Key.shift, keyboard.KeyCode(char='x')},
{keyboard.Key.ctrl_r, keyboard.Key.shift, keyboard.KeyCode(char='x')},
{keyboard.Key.ctrl_l, keyboard.Key.shift, keyboard.KeyCode(char='z')},
{keyboard.Key.ctrl_r, keyboard.Key.shift, keyboard.KeyCode(char='z')},
{keyboard.Key.ctrl_l, keyboard.Key.shift, keyboard.KeyCode(char='v')},
{keyboard.Key.ctrl_r, keyboard.Key.shift, keyboard.KeyCode(char='v')},
{keyboard.Key.ctrl_l, keyboard.Key.shift, keyboard.KeyCode(char='b')},
{keyboard.Key.ctrl_r, keyboard.Key.shift, keyboard.KeyCode(char='b')},
{keyboard.Key.ctrl_l, keyboard.Key.shift, keyboard.KeyCode(char='e')},
{keyboard.Key.ctrl_r, keyboard.Key.shift, keyboard.KeyCode(char='e')},
{keyboard.Key.ctrl_l, keyboard.Key.shift, keyboard.KeyCode(char='d')},
{keyboard.Key.ctrl_r, keyboard.Key.shift, keyboard.KeyCode(char='d')},
{keyboard.Key.ctrl_l, keyboard.Key.shift, keyboard.KeyCode(char='v')},
{keyboard.Key.ctrl_r, keyboard.Key.shift, keyboard.KeyCode(char='v')},
{keyboard.Key.esc}
]
# The currently active modifiers
current = set()
tnow = datetime.datetime.now()
tcounter = 0
def on_press(key):
if any([key in comb for comb in COMBINATIONS]):
current.add(key)
if any(all(k in current for k in comb) for comb in COMBINATIONS):
global tnow
global tcounter
tcounter += 1
if datetime.datetime.now() - tnow < datetime.timedelta(seconds=1):
if tcounter > 1:
tcounter = 0
main_function()
else:
tnow = datetime.datetime.now()
if key == keyboard.Key.esc:
listener.stop()
def on_release(key):
try:
current.remove(key)
except KeyError:
pass
def main_function():
if (key in COMBO for COMBO in COMBINATIONS) == "keyboard.Key.ctrl_l, keyboard.Key.shift, keyboard.KeyCode(char='c')" or "keyboard.Key.ctrl_r, keyboard.Key.shift, keyboard.KeyCode(char='c')":
keyboard.press(('x'), Key.enter, Key.space)
elif (key in COMBO for COMBO in COMBINATIONS) == "keyboard.Key.ctrl_l, keyboard.Key.shift, keyboard.KeyCode(char='x')" or "keyboard.Key.ctrl_r, keyboard.Key.shift, keyboard.KeyCode(char='x')":
keyboard.press(('f'), Key.enter, Key.space)
elif (key in COMBO for COMBO in COMBINATIONS) == "keyboard.Key.ctrl_l, keyboard.Key.shift, keyboard.KeyCode(char='z')" or "keyboard.Key.ctrl_r, keyboard.Key.shift, keyboard.KeyCode(char='z')":
keyboard.press(('m'), Key.enter, Key.space)
elif (key in COMBO for COMBO in COMBINATIONS) == "keyboard.Key.ctrl_l, keyboard.Key.shift, keyboard.KeyCode(char='v')" or "keyboard.Key.ctrl_r, keyboard.Key.shift, keyboard.KeyCode(char='v')":
document.getElementById("unenrolled").click
elif (key in COMBO for COMBO in COMBINATIONS) == "keyboard.Key.ctrl_l, keyboard.Key.shift, keyboard.KeyCode(char='b')" or "keyboard.Key.ctrl_r, keyboard.Key.shift, keyboard.KeyCode(char='b')":
document.getElementById("unsure").click
elif (key in COMBO for COMBO in COMBINATIONS) == "keyboard.Key.ctrl_l, keyboard.Key.shift, keyboard.KeyCode(char='e')" or "keyboard.Key.ctrl_r, keyboard.Key.shift, keyboard.KeyCode(char='e')":
document.getElementById("mediaSpeech").click
elif (key in COMBO for COMBO in COMBINATIONS) == "keyboard.Key.ctrl_l, keyboard.Key.shift, keyboard.KeyCode(char='d')" or "keyboard.Key.ctrl_r, keyboard.Key.shift, keyboard.KeyCode(char='d')":
document.getElementById("backgroundSpeech").click
elif (key in COMBO for COMBO in COMBINATIONS) == "keyboard.Key.ctrl_l, keyboard.Key.shift, keyboard.KeyCode(char='v')" or "keyboard.Key.ctrl_l, keyboard.Key.shift, keyboard.KeyCode(char='v')":
document.getElementById("criticalData").click
else:
return
with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:
listener.join()
感谢您的帮助。