How to simulate mouse movements using Python?

When it comes to automation, be it to setup your computer on start-up or to farm coins on a clicker game, it becomes essential to simulate mouse movements and clicks. And what better way to do this than use Python!

For performing this particular task of automating or simulating your mouse movement, we will be using Python's mouse library that has various methods and functionalities to help simulate your mouse on your computer.

Installation and Setup

First, we need to install the mouse library since it doesn't come pre-packaged with Python ?

pip install mouse

Once installed, import the module in your Python script ?

import mouse
import time

Using this module, we can hook global events, register hotkeys, simulate mouse movements, clicks and much more.

Getting Mouse Position

You can retrieve the current position of your mouse cursor using the get_position() method ?

import mouse

# Get current mouse position
position = mouse.get_position()
print(f"Current mouse position: {position}")
Current mouse position: (731, 465)

This returns a tuple containing the X and Y coordinates of the mouse cursor on your screen.

Simulating Mouse Movement

Move the mouse to a specific position using the move() method ?

import mouse
import time

# Move mouse to position (150, 150) instantly
mouse.move(150, 150)
print("Mouse moved to (150, 150)")

# Move with duration for smooth movement
mouse.move(300, 300, duration=2.0)
print("Mouse moved to (300, 300) in 2 seconds")
Mouse moved to (150, 150)
Mouse moved to (300, 300) in 2 seconds

The duration parameter creates smooth, animated movement over the specified time period.

Simulating Mouse Clicks

Simulate different types of mouse clicks using the click() method ?

import mouse
import time

# Different types of clicks
mouse.click('left')      # Left click
print("Left click performed")

time.sleep(1)
mouse.click('right')     # Right click  
print("Right click performed")

time.sleep(1)
mouse.click('middle')    # Middle mouse button click
print("Middle click performed")
Left click performed
Right click performed
Middle click performed

Event Handling

Execute functions when mouse events occur using event handlers ?

import mouse

def on_click_handler():
    print("Mouse clicked!")

# Register click event handler
mouse.on_click(on_click_handler)
print("Click event handler registered. Click anywhere to test.")

# Note: In real usage, you'd need to keep the program running
# For demo purposes, we'll just show the registration
Click event handler registered. Click anywhere to test.
Mouse clicked!

Simulating Mouse Scrolling

Control mouse wheel scrolling with the wheel() method ?

import mouse
import time

# Scroll up (positive values)
mouse.wheel(3)
print("Scrolled up 3 units")

time.sleep(1)

# Scroll down (negative values)
mouse.wheel(-2)
print("Scrolled down 2 units")
Scrolled up 3 units
Scrolled down 2 units

Positive values scroll up, while negative values scroll down.

Complete Example

Here's a practical example that combines multiple mouse operations ?

import mouse
import time

def automate_mouse_sequence():
    print("Starting mouse automation sequence...")
    
    # Get current position
    start_pos = mouse.get_position()
    print(f"Starting position: {start_pos}")
    
    # Move to different positions and click
    positions = [(100, 100), (200, 200), (300, 150)]
    
    for pos in positions:
        mouse.move(pos[0], pos[1], duration=1.0)
        print(f"Moved to {pos}")
        mouse.click('left')
        print(f"Clicked at {pos}")
        time.sleep(0.5)
    
    # Return to starting position
    mouse.move(start_pos[0], start_pos[1], duration=1.0)
    print(f"Returned to starting position: {start_pos}")

# Run the automation (uncomment to execute)
# automate_mouse_sequence()
print("Mouse automation sequence defined")
Mouse automation sequence defined

Key Methods Summary

Method Purpose Example
get_position() Get current mouse position mouse.get_position()
move(x, y) Move mouse to coordinates mouse.move(100, 200)
click(button) Simulate mouse click mouse.click('left')
wheel(delta) Simulate mouse scroll mouse.wheel(3)
on_click(func) Register click event handler mouse.on_click(callback)

Conclusion

The Python mouse library provides simple and effective methods for simulating mouse movements, clicks, and scrolls. While alternatives like PyAutoGUI or pynput exist, the mouse library offers straightforward functionality that's perfect for basic automation tasks.

Updated on: 2026-03-27T11:12:12+05:30

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements