Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
915 views
in Technique[技术] by (71.8m points)

python - Unhandled exception in listener callback computer stopped program after waking up from sleep mode


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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...