Here's the updated Python script that makes the mouse movement sequence loop indefinitely until you press the 'J' key. I've incorporated keyboard detection using the pynput library to monitor for the 'J' key press. This allows the script to run in a loop while checking for the key in the background.
Key Changes:
The script now loops the entire sequence (initial click and movements with clicks) using a while loop.
A global flag (running) controls the loop. When you press 'J', the flag is set to False, stopping the loop.
I've used pynput for keyboard listening, as it runs in a separate thread and won't block the mouse movements.
The 3-second wait for initial positioning only happens once at the start, before the loop begins.
Requirements:
Install the required libraries if you haven't already:
code
pip install pyautogui pynput
Important Notes:
Pressing 'J' (lowercase or uppercase should work, but it's case-sensitive to lowercase 'j' in this setup) will stop the loop.
Be cautious: This script will keep moving your mouse and clicking until you stop it, so run it in a safe environment.
If the script doesn't respond to key presses immediately, it might be due to system focus; ensure the script window is not blocking input.
As always, use the failsafe: Move your mouse to the top-left corner of the screen at any time to force pyautogui to raise an exception and stop.
Here's the script:
python
import pyautogui
import time
from pynput import keyboard
# Set the distance to move in each direction
distance = 100 # pixels
duration = 0.5 # seconds for each move
running = True # Global flag to control the loop
def on_press(key):
global running # Access the global variable
try:
if key.char == 'j': # Check if the pressed key is 'j'
print("J pressed! Stopping the loop...")
running = False # Set the flag to False to exit the loop
except AttributeError:
# Ignore keys without a 'char' attribute (e.g., shift, ctrl)
pass
# Start the keyboard listener in a separate thread
listener = keyboard.Listener(on_press=on_press)
listener.start()
# Give user time to move mouse to starting position (only once)
print("Starting in 3 seconds. Move your mouse to the starting position and press Ctrl+C if needed.")
time.sleep(3)
while running:
# Initial left click at the starting position
pyautogui.click()
# Move down-right and click
pyautogui.moveRel(distance, distance, duration=duration)
pyautogui.click()
# Move down-left and click
pyautogui.moveRel(-distance, distance, duration=duration)
pyautogui.click()
# Move up-left and click
pyautogui.moveRel(-distance, -distance, duration=duration)
pyautogui.click()
# Move up-right and click (back to starting position)
pyautogui.moveRel(distance, -distance, duration=duration)
pyautogui.click()
# After the loop ends, stop the listener
listener.stop()
print("Loop cancelled! The script has stopped.")
How it Works:
Setup: The script starts by waiting 3 seconds for you to position your mouse.
Loop: It enters a while loop that repeatedly performs the sequence:
Clicks at the current position.
Moves down-right and clicks.
Moves down-left and clicks.
Moves up-left and clicks.
Moves up-right and clicks (returning to the start).
Stopping the Loop: The keyboard listener runs in the background. As soon as you press 'J', it sets running to False, and the loop stops after the current sequence finishes.
End: The script stops the listener and prints a confirmation message.
If you encounter any issues (e.g., the key press not registering), let me know for further tweaks! For example, I can adjust it to detect uppercase 'J' or add more key options. 🚀