Python Script to Keep Windows Alive
import time
import ctypes
from datetime import datetime
# Win32 API key event codes
KEYEVENTF_KEYUP = 0x0002
VK_SHIFT = 0x10 # Virtual-key code for SHIFT key
def press_key(vk_code):
# Press key down
ctypes.windll.user32.keybd_event(vk_code, 0, 0, 0)
time.sleep(0.05)
# Release key
ctypes.windll.user32.keybd_event(vk_code, 0, KEYEVENTF_KEYUP, 0)
if __name__ == "__main__":
runtime_hours = 9
runtime_seconds = runtime_hours * 60 * 60
start_time = time.time()
end_time = start_time + runtime_seconds
# Print planned stop time
print(f"Keep-alive script running for {runtime_hours} hours.")
stop_time_str = datetime.fromtimestamp(end_time).strftime("%Y-%m-%d %H:%M:%S")
print(f"Will stop at: {stop_time_str}\n")
while True:
press_key(VK_SHIFT) # simulate pressing SHIFT
now_str = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print(f"[{now_str}] Pressed SHIFT to keep system awake")
time.sleep(60) # wait 60 seconds
if time.time() >= end_time:
print("9 hours completed. Exiting...")
break
Comments
Post a Comment