Last active
June 1, 2023 08:38
-
-
Save willwade/cb666ed0f057cc426bbf89604efcd8ff to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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