Skip to content

Instantly share code, notes, and snippets.

@tahajalili
Created May 13, 2026 15:09
Show Gist options
  • Select an option

  • Save tahajalili/e1ebc7104f9a5c566aacbcda4ccc20d5 to your computer and use it in GitHub Desktop.

Select an option

Save tahajalili/e1ebc7104f9a5c566aacbcda4ccc20d5 to your computer and use it in GitHub Desktop.
Windows -> Save as afk.pyw and double click on it to run headlessly.
#!/usr/bin/env python3
import sys
import os
import time
import random
import platform
try:
import pyautogui
except ImportError:
sys.exit(1)
# --- Headless setup ---
if platform.system() == "Windows":
import ctypes
hwnd = ctypes.windll.kernel32.GetConsoleWindow()
if hwnd:
ctypes.windll.user32.ShowWindow(hwnd, 0)
# Silence all output
sys.stdout = open(os.devnull, "w")
sys.stderr = open(os.devnull, "w")
# --- Config ---
INTERVAL = 30
INTERVAL_RAND = 5
MOVE_PX = 10
JITTER_PX = 3
OUT_DURATION = 0.2
BACK_DURATION = 0.2
pyautogui.FAILSAFE = True
# --- Main ---
def main():
while True:
sleep_time = INTERVAL + random.uniform(-INTERVAL_RAND, INTERVAL_RAND)
time.sleep(max(1, sleep_time))
x, y = pyautogui.position()
dx = random.randint(-MOVE_PX - JITTER_PX, MOVE_PX + JITTER_PX)
dy = random.randint(-MOVE_PX - JITTER_PX, MOVE_PX + JITTER_PX)
pyautogui.moveTo(x + dx, y + dy, duration=OUT_DURATION)
pyautogui.moveTo(x, y, duration=BACK_DURATION)
if __name__ == "__main__":
try:
main()
except (pyautogui.FailSafeException, KeyboardInterrupt):
sys.exit(0)
except Exception:
sys.exit(1)
@tahajalili

Copy link
Copy Markdown
Author

Move the cursor to any corner to kill the process.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment