Skip to content

Instantly share code, notes, and snippets.

@willwade
Last active June 1, 2023 08:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willwade/cb666ed0f057cc426bbf89604efcd8ff to your computer and use it in GitHub Desktop.
Save willwade/cb666ed0f057cc426bbf89604efcd8ff to your computer and use it in GitHub Desktop.
from pynput.mouse import Controller
from pynput.keyboard import Listener, Key
import time
# Create an instance of the mouse controller
mouse = Controller()
# Set the initial position of the cursor
x, y = mouse.position
# Define the square's size
square_size = 100
# Variable to keep track of the loop
running = True
def on_press(key):
global running
if key == Key.esc: # Press 'Esc' key to exit the program
running = False
return False
def move_square():
global running
while running:
for _ in range(4):
mouse.position = (x + square_size, y)
time.sleep(1)
mouse.position = (x + square_size, y + square_size)
time.sleep(1)
mouse.position = (x, y + square_size)
time.sleep(1)
mouse.position = (x, y)
time.sleep(1)
if not running:
break
# Start listening for key presses in a separate thread
with Listener(on_press=on_press) as listener:
move_square()
# Restore the cursor to the original position
mouse.position = (x, y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment