简体   繁体   中英

Exiting out of a running Python script when script window is hidden

I have a Python script written using Visual Studio Code that periodically moves the mouse icon to keep the computer active and stop it from falling asleep. This computer is used for displaying other info, so when the program is running the script window is minimized and another window is selected and opened and then the computer is left alone. When I want to use the computer again the Ctrl+C keyboard interrupt to terminate the script does not work unless the script window is reopened. This proved to be annoying as the script takes control of the mouse away from the user. I have a 'time.sleep' command that pauses the mouse movement to allow for user control again for a bit, but I would like to be ab le to just exit the script without having to wait for this pause to be able to open up the script window.

I am fairly new to Python, so I am unsure of other commands or keyboard inputs that might allow this to be possible.

The code I have does utilize the pyautogui module and has the default failsafe of moving the mouse icon to the corner of the screen enabled. However, this still requires me to wait for the pause in the script when I have control of the mouse again.

This is for a Windows environment.

If you are using pyautogui, just quickly move your mouse to the top left corner of the screen and your program will stop

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM