I am writing a python script to put my computer into sleep mode, whenever I press the hotkey cmd + enter
which is the same as the windows key and the enter key at the same time. The program puts the computer into sleep mode just fine, but when I wake it up again and unlock the device an error occurred and the program stopped due to this error:
Unhandled exception in listener callback
My script just pressed the shortcut cmd + d
which goes straight to the desktop and then presses alt + f4
followed by enter
.
The Code:
from pynput import keyboard
import time
import pyautogui
# The key combination to check
COMBINATIONS = [
{keyboard.Key.cmd, keyboard.Key.enter}
]
current = set()
def execute():
print("Shortcut pressed")
pyautogui.hotkey('win', 'd')
pyautogui.hotkey('alt', 'f4')
time.sleep(0.2)
pyautogui.keyDown('up')
pyautogui.keyUp('up')
time.sleep(0.1)
pyautogui.keyDown('enter')
def on_press(key):
if any([key in COMBO for COMBO in COMBINATIONS]):
current.add(key)
if any(all(k in current for k in COMBO) for COMBO in COMBINATIONS):
execute()
'''
def on_release(key):
if any([key in COMBO for COMBO in COMBINATIONS]):
current.remove(key)
'''
with keyboard.Listener(on_press=on_press) as listener:
listener.join()
So the problem is that the program ends its execution when the pc wakes up from sleep mode.
Question:
How can I avoid that the program throws an exception after waking up from sleep mode or is it impossible because every running prcess gets stopped when I put the system into standy?
question from:
https://stackoverflow.com/questions/65887202/unhandled-exception-in-listener-callback-computer-stopped-program-after-waking-u 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…