In the power settings, my computer is configured to turn off the screen after 2 hours of inactivity.
In all of my Python projetcs that I run at random times during my day or my life, I add a function that runs in a separate thread and toggles the keyboard’s “Num Lock” off and on every hour:
def wakelock() -> None:
keyboard = Controller()
keyboard.press(Key.num_lock)
keyboard.release(Key.num_lock)
keyboard.press(Key.num_lock)
keyboard.release(Key.num_lock)
This way, I keep the screen on while the code is running, it doesn’t create any real risk of interfering with typing or mouse movements, and I don’t need to change the power settings to “Never turn off” and risk the power going out and the wrong configuration staying that way without me even remembering.
That said, I would like to know if there is a way for Windows 11 itself to check whether there is any Python process running, and if there is, tell the system to keep the screen active; if there isn’t, then not send that signal.