Skip to content

Instantly share code, notes, and snippets.

@lboulard
Last active December 7, 2023 19:12
Show Gist options
  • Save lboulard/a4c5445e219d146f4191931527233f4c to your computer and use it in GitHub Desktop.
Save lboulard/a4c5445e219d146f4191931527233f4c to your computer and use it in GitHub Desktop.
Prevent windows from powering off screen #windows
#!/usr/bin/env python3
import ctypes
import signal
import threading
if __name__ == "__main__":
SetThreadExecutionState = ctypes.windll.kernel32.SetThreadExecutionState
SetThreadExecutionState.argtypes = [ctypes.c_uint32]
# https://bugs.python.org/issue35935#msg335056
signal.signal(signal.SIGINT, signal.SIG_DFL)
signal.signal(signal.SIGBREAK, signal.SIG_DFL)
try:
# Replace with 0x80000003 to also reset user idle timer.
# Then this prevents device from entering deep power saving or hibernate.
state = 0x80000002
SetThreadExecutionState(state)
exit = threading.Event()
while not exit.is_set():
exit.wait()
except KeyboardInterrupt:
pass
finally:
SetThreadExecutionState(state)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment