Take control of the mouse
A Controller instance from pynput.mouse gives you programmatic access to the cursor β same input layer as a real user.
A tiny Python script that programmatically moves your cursor with random jitter and delay β useful for testing input handling, screencasts, or just experimenting with automation.
The script
Drop it in a .py file and run it. Press Ctrl+C to stop.
import time
import random
from pynput.mouse import Controller, Listener
mouse = Controller()
def simulate_lag():
for _ in range(10): # Simulating lag 10 times
mouse.move(random.randint(-5, 5), random.randint(-5, 5))
time.sleep(random.uniform(0.1, 0.5)) # Random delays to simulate lag
def simulate_zoom():
print('Zooming in...')
# In a real implementation, here you'd add code to zoom in on the screen
if __name__ == '__main__':
print('Press Ctrl+C to stop the simulation')
try:
simulate_lag()
simulate_zoom()
except KeyboardInterrupt:
print('Stopped by user') How it works
A Controller instance from pynput.mouse gives you programmatic access to the cursor β same input layer as a real user.
Each loop iteration nudges the cursor by a random pixel offset between β5 and +5 on both axes.
A 0.1β0.5 second pause between moves creates an organic, laggy rhythm β not a clean loop.
Install
Install pynput with pip, then run the script. macOS users may need to grant Accessibility permissions to your terminal app.
Copy the script, install pynput, and watch your cursor dance.
Copy the script