How to control your mouse with Python

Move mouse cursor every minute #

Python script that uses the pyautogui library to move the mouse cursor every minute:
This script requires the pyautogui library, which you can install by running pip install pyautogui in your command prompt or terminal.

pip install pyautogui
import pyautogui
import time

while True:
# Get the current mouse position
current_x, current_y = pyautogui.position()

# Move the mouse cursor slightly to the right
pyautogui.moveTo(current_x + 10, current_y)

# Wait for 60 seconds
time.sleep(60)

In this script, we use a while loop to keep the mouse moving indefinitely. Within the loop, we first get the mouse cursor's current position using pyautogui.position(). Then we move the cursor slightly to the right using pyautogui.moveTo() function. You can modify the movement distance according to your preference by changing the + 10 in the moveTo() function.

After moving the mouse, we use time.sleep(60) to pause the script for 60 seconds (1 minute). This ensures that the script will move the mouse every minute.

When running the script, make sure you keep your mouse cursor within the screen boundaries to prevent it from moving off the screen.

You can add modifications to this script.

Here are a few examples of modifications you can make to the script to perform different actions with the mouse:

Simulate Mouse Click: #

You can use the pyautogui.click() function to simulate a mouse click at the current cursor position. Here's an example of how you can modify the script to perform a left mouse click every minute:

import pyautogui
import time

while True:
pyautogui.click() # Perform a left mouse click

# Wait for 60 seconds
time.sleep(60)

Move Mouse to Specific Coordinates: #

Instead of moving the mouse cursor by a fixed distance, you can move it to specific coordinates on the screen using pyautogui.moveTo() function. Here's an example that moves the mouse cursor to the coordinates (500, 500) every minute:

import pyautogui
import time

while True:
pyautogui.moveTo(500, 500) # Move the mouse cursor to (500, 500)

# Wait for 60 seconds
time.sleep(60)

Scroll the Mouse Wheel: #

You can also simulate scrolling of the mouse wheel using pyautogui.scroll() function. Here's an example that scrolls the mouse wheel down by 100 units every minute:

import pyautogui
import time

while True:
pyautogui.scroll(-100) # Scroll the mouse wheel down by 100 units

# Wait for 60 seconds
time.sleep(60)

Certainly! Here are a few more examples of modifications you can make to the script to perform different actions with the mouse:

Drag and Drop: #

You can simulate a drag-and-drop action by using the pyautogui.dragTo() function to move the mouse cursor while holding down the mouse button. Here's an example that performs a drag-and-drop from coordinates (100, 100) to (200, 200) every minute:

import pyautogui
import time

while True:
pyautogui.moveTo(100, 100) # Move to the starting position
pyautogui.mouseDown() # Hold down the mouse button
pyautogui.moveTo(200, 200, duration=1) # Move to the target position
pyautogui.mouseUp() # Release the mouse button

# Wait for 60 seconds
time.sleep(60)

Right Mouse Click: #

You can simulate a right mouse click by using the pyautogui.rightClick() function. Here's an example that performs a right mouse click at the current cursor position every minute:

import pyautogui
import time

while True:
pyautogui.rightClick() # Perform a right mouse click

# Wait for 60 seconds
time.sleep(60)

Double Click: #

You can simulate a double click by using the pyautogui.doubleClick() function. Here's an example that performs a double click at the current cursor position every minute:

import pyautogui
import time

while True:
pyautogui.doubleClick() # Perform a double click

# Wait for 60 seconds
time.sleep(60)

These are just a few examples of how you can modify the script to perform different actions with the mouse. You can explore the pyautogui library documentation for more functions and options to control mouse movements and actions.

Practical examples of usage the mouse moving acripts #

Automated Presentations or Demos:

  • Use the script that performs a mouse click or double click to automate presentations or demos.
  • Simulate mouse actions like clicking on buttons or navigating through slides at predefined intervals to create an automated presentation.

Preventing Screensaver Activation:

  • Use the script that moves the mouse cursor slightly every minute to prevent the screensaver from activating.
  • Keeping the system active by moving the mouse prevents the screensaver from kicking in due to inactivity.

Gaming or Simulation:

  • Modify the script to automate repetitive mouse actions in gaming or simulation scenarios.
  • Automate actions like clicking or dragging in games or simulations that require continuous input.

Preventing System Lock or Sleep:

  • Use the script to prevent systems from going into lock or sleep mode due to inactivity.
  • Moving the mouse cursor or performing mouse actions at regular intervals keeps the system active.

Automated Data Entry or Form Filling:

  • Modify the script to automate repetitive data entry tasks or form filling requirements.
  • Move the mouse cursor to specific fields and perform mouse actions like clicking or dragging to enter or select data automatically.

Published