Skip to content

Instantly share code, notes, and snippets.

@mura303
Created September 22, 2025 12:37
Show Gist options
  • Select an option

  • Save mura303/d1f428d4e3ad0dff209ca7e297a599d9 to your computer and use it in GitHub Desktop.

Select an option

Save mura303/d1f428d4e3ad0dff209ca7e297a599d9 to your computer and use it in GitHub Desktop.
import argparse
import ctypes
import random
import time
class POINT(ctypes.Structure):
_fields_ = [("x", ctypes.c_long), ("y", ctypes.c_long)]
def _get_cursor_pos():
point = POINT()
if not ctypes.windll.user32.GetCursorPos(ctypes.byref(point)):
raise ctypes.WinError()
return point
def _set_cursor_pos(x, y):
if not ctypes.windll.user32.SetCursorPos(int(x), int(y)):
raise ctypes.WinError()
def move_mouse(offset_x, offset_y):
"""Move cursor by the given offset in screen coordinates."""
point = _get_cursor_pos()
_set_cursor_pos(point.x + offset_x, point.y + offset_y)
def jiggle(interval=45.0, jitter=2.0):
"""Periodically nudge the mouse cursor to prevent idle detection."""
print(
f"Jiggling mouse every {interval} seconds with +/-{jitter} px jitter. Press Ctrl+C to stop."
)
try:
while True:
dx = random.choice((-1, 1)) * random.uniform(0.5, jitter)
dy = random.choice((-1, 1)) * random.uniform(0.5, jitter)
move_mouse(dx, dy)
time.sleep(0.25)
move_mouse(-dx, -dy)
time.sleep(interval)
except KeyboardInterrupt:
print("Stopped mouse jiggle.")
def parse_args():
parser = argparse.ArgumentParser(
description="Periodically move the mouse to prevent the screen saver."
)
parser.add_argument(
"--interval",
type=float,
default=45.0,
help="Seconds to wait between jiggles (default: 45).",
)
parser.add_argument(
"--jitter",
type=float,
default=2.0,
help="Maximum pixel offset per jiggle (default: 2).",
)
return parser.parse_args()
def main():
args = parse_args()
jiggle(interval=args.interval, jitter=args.jitter)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment