Skip to content

Instantly share code, notes, and snippets.

@totalhack
Created April 14, 2020 14:06
Show Gist options
  • Save totalhack/4945aa87af995e624a235aa11a7771df to your computer and use it in GitHub Desktop.
Save totalhack/4945aa87af995e624a235aa11a7771df to your computer and use it in GitHub Desktop.
Prevent your computer from appearing idle by dancing the mouse around.
"""
pip install art pynput
"""
import os
import random
import time
from art import tprint
import pynput
MAX_IDLE_TIME = 20 # Dance if idle for N seconds
IDLE_CHECK_SLEEP = 1 # Check if idle every N seconds
MOVEMENT_SLEEP = .5 # Move every N seconds while dancing
POINT_SPACING = 50
XMIN, YMIN = (50, 50)
XMAX, YMAX = (1000, 800)
X_TICKS = list(range(XMIN, XMAX + POINT_SPACING, POINT_SPACING))
Y_TICKS = list(range(YMIN, YMAX + POINT_SPACING, POINT_SPACING))
POINTS = [(x, y) for x in X_TICKS for y in Y_TICKS]
idle_time = 0
mouse = pynput.mouse.Controller()
def clear():
os.system('cls' if os.name == 'nt' else 'clear')
def dance():
while True:
clear()
tprint("Dance!", "random")
x, y = random.choice(POINTS)
new_position = (x, y)
mouse.position = new_position
time.sleep(MOVEMENT_SLEEP)
if mouse.position != new_position:
clear()
tprint("Stop!")
return
clear()
tprint("Waiting...")
while True:
start = time.time()
position = mouse.position
time.sleep(IDLE_CHECK_SLEEP)
if mouse.position == position:
# No movement, check how long we've been idle
idle_time += (time.time() - start)
if idle_time > MAX_IDLE_TIME:
dance()
idle_time = 0
else:
# Mouse moved, reset idle counter
idle_time = 0
@totalhack
Copy link
Author

Open a terminal, install dependencies, and run python dance.py. If your mouse doesn't move for 20 seconds it will start dancing around the screen. If you move the mouse again it will stop and reset the idle timer. You should appear as active on desktop apps that check whether you are idle, such as Slack (desktop app only, not the web or mobile apps).

Tested on Mac OS X 10.12 and 10.14. You may need to give Terminal special permissions to do this.

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