Я новичок в 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()
Спасибо за вашу помощь.