Skip to content

Instantly share code, notes, and snippets.

@johnscillieri
Created January 6, 2026 15:53
Show Gist options
  • Select an option

  • Save johnscillieri/2de915a0d260e20cee5445a40444f972 to your computer and use it in GitHub Desktop.

Select an option

Save johnscillieri/2de915a0d260e20cee5445a40444f972 to your computer and use it in GitHub Desktop.
# keep_awake_windows.py
import time
import argparse
import ctypes
from ctypes import wintypes
# --- Win32 API setup ---
user32 = ctypes.windll.user32
kernel32 = ctypes.windll.kernel32
class LASTINPUTINFO(ctypes.Structure):
_fields_ = [("cbSize", wintypes.UINT),
("dwTime", wintypes.DWORD)]
def get_idle_seconds() -> float:
lii = LASTINPUTINFO()
lii.cbSize = ctypes.sizeof(LASTINPUTINFO)
if not user32.GetLastInputInfo(ctypes.byref(lii)):
raise ctypes.WinError()
now = kernel32.GetTickCount() # 32-bit
idle_ms = (now - lii.dwTime) & 0xFFFFFFFF # handle wraparound
return idle_ms / 1000.0
def nudge_mouse(px: int):
"""Move the mouse by px and back using SendInput-compatible mouse_event."""
MOUSEEVENTF_MOVE = 0x0001
user32.mouse_event(MOUSEEVENTF_MOVE, px, 0, 0, 0)
user32.mouse_event(MOUSEEVENTF_MOVE, -px, 0, 0, 0)
print("bump.")
def main():
ap = argparse.ArgumentParser(description="Nudge mouse after inactivity (Windows).")
ap.add_argument("--minutes", type=float, default=4, help="Idle minutes before nudging.")
ap.add_argument("--pixels", type=int, default=1, help="Pixels to move for the nudge.")
ap.add_argument("--interval", type=float, default=30, help="Seconds between checks.")
args = ap.parse_args()
threshold = args.minutes * 60.0
print(f"Monitoring idle time. Nudge after {args.minutes} min of inactivity.")
try:
while True:
idle = get_idle_seconds()
print(f"{idle} vs {threshold}...")
if idle >= threshold:
nudge_mouse(args.pixels)
# Reset cadence so we don't spam every loop cycle
time.sleep(args.interval)
time.sleep(args.interval)
except KeyboardInterrupt:
print("Exiting…")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment