Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import ctypes
- import time
- # Constants from the Windows API
- ES_CONTINUOUS = 0x80000000
- ES_SYSTEM_REQUIRED = 0x00000001
- ES_DISPLAY_REQUIRED = 0x00000002
- def prevent_sleep():
- # Prevent the system from sleeping and the display from turning off
- ctypes.windll.kernel32.SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED | ES_DISPLAY_REQUIRED)
- def allow_sleep():
- # Allow the system and display to sleep again
- ctypes.windll.kernel32.SetThreadExecutionState(ES_CONTINUOUS)
- try:
- prevent_sleep()
- print("System sleep and display off prevented. Press Ctrl+C to allow sleep again.")
- while True:
- time.sleep(60) # Keep the script running
- except KeyboardInterrupt:
- allow_sleep()
- print("System and display sleep allowed.")
Advertisement
Add Comment
Please, Sign In to add comment