I have written a small program, that keeps my computers awake even, when the battery settings can not be changed: https://github.com/xlaech/Anti-Lock-5000. My current setup uses clicks to reset the idle timer.
I was asking myself: Since there is a struct that saves the idle time (https://docs.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-lastinputinfo) is there a way to override this value, keeping the computer from going to sleep without any action visible to the user?
While a solution in python is not necessary - it needs to be a portable solution (e.g. no libraries, no downloads).
CodePudding user response:
Using SetThreadExecutionState (as mentioned by @IInspectable):
import time
from ctypes import windll
ES_CONTINUOUS = 0x80000000 # Causes the set state to remain
ES_DISPLAY_REQUIRED = 0x00000002 # Forces the display to be on
flags = ES_CONTINUOUS | ES_DISPLAY_REQUIRED
windll.kernel32.SetThreadExecutionState(flags)
while True:
time.sleep(36000) # Keep process alive, but sleep to prevent system load
