Mouse Control Library for Python: Automation & Macros

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction

Mouse control is a key aspect of automating user interface interactions. Whether you're working on automated tests, scripts, game bots, or utility automation, you often need to programmatically move the cursor, simulate clicks, and capture user actions. In Python, the mouse library is an excellent choice for this.

The mouse library provides a simple, straightforward API for working with the mouse: event tracking, click execution, and recording and replaying movements. It's cross-platform, easy to install, and works well in both standalone scripts and complex RPA projects.

Installing and Setting Up the Mouse Library

Installation via pip

pip install mouse

System Requirements & Permissions

On Linux and macOS, you may need to run as administrator or grant input control permissions.

For Linux:

sudo pip install mouse

For macOS: Grant access in Security & Privacy → Accessibility settings.

For Windows: Some features require running with administrator privileges.

Architecture & How It Works

Core Components

The mouse library consists of several key components:

  • Controller — manages cursor movement and action emulation
  • Listener — tracks mouse events
  • Recorder — records and replays macros
  • Event System — handles event processing

Supported Platforms

  • Windows (via Windows API)
  • macOS (via Cocoa)
  • Linux (via X11/Wayland)

Key Features of the Library

Mouse Action Emulation

The mouse library works both as a listener for user actions and as a controller for emulating movements and clicks.

Main functions:

  • Track button presses and releases
  • Get cursor position
  • Programmatic clicks and scrolling
  • Move the cursor
  • Mouse hotkeys
  • Record and replay macros

Event Types

The library handles the following event types:

  • ButtonEvent — button press/release
  • MoveEvent — cursor movement
  • WheelEvent — scroll wheel action

Cursor Control & Positioning

Moving the Cursor

import mouse

# Absolute positioning
mouse.move(100, 200)

# Relative movement
mouse.move(50, 0, absolute=False)

# Smooth movement with delay
mouse.move(300, 400, duration=1.0)

Getting Current Coordinates

# Get cursor position
x, y = mouse.get_position()
print(f"Cursor is at: ({x}, {y})")

# Use in conditions
if mouse.get_position()[0] > 500:
    print("Cursor is on the right side of the screen")

Emulating Clicks & Actions

Basic Click Types

# Left click
mouse.click('left')
mouse.left_click()

# Right click
mouse.click('right')
mouse.right_click()

# Middle click
mouse.click('middle')
mouse.middle_click()

# Double click
mouse.double_click()

Clicks at Specific Coordinates

# Click at specific coordinates
mouse.click('left', (300, 400))

# Save position before clicking
original_pos = mouse.get_position()
mouse.click('left', (100, 100))
mouse.move(*original_pos)

Holding

Recommendations