PashaM999

Prevent sleep Windows

Jul 25th, 2024 (edited)
575
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.77 KB | Source Code | 0 0
  1. import ctypes
  2. import time
  3.  
  4. # Constants from the Windows API
  5. ES_CONTINUOUS = 0x80000000
  6. ES_SYSTEM_REQUIRED = 0x00000001
  7. ES_DISPLAY_REQUIRED = 0x00000002
  8.  
  9. def prevent_sleep():
  10.     # Prevent the system from sleeping and the display from turning off
  11.     ctypes.windll.kernel32.SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED | ES_DISPLAY_REQUIRED)
  12.  
  13. def allow_sleep():
  14.     # Allow the system and display to sleep again
  15.     ctypes.windll.kernel32.SetThreadExecutionState(ES_CONTINUOUS)
  16.  
  17. try:
  18.     prevent_sleep()
  19.     print("System sleep and display off prevented. Press Ctrl+C to allow sleep again.")
  20.     while True:
  21.         time.sleep(60)  # Keep the script running
  22. except KeyboardInterrupt:
  23.     allow_sleep()
  24.     print("System and display sleep allowed.")
  25.  
Advertisement
Add Comment
Please, Sign In to add comment