Mouse - mouse management

онлайн тренажер по питону
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 crucial aspect of automating user‑interface interactions. In many scenarios—whether automated tests, scripts, game bots, or automation utilities—programmatic cursor movement, click simulation, and user‑action interception are required. In Python, the mouse library perfectly fits these needs.

The mouse library offers a simple, straightforward API for mouse operations: event listening, click execution, motion recording and playback. It is cross‑platform, easy to install, and works both in isolated scripts and complex RPA projects.

Installation and Setup of the mouse Library

Installation via pip

pip install mouse

System Requirements and Permissions

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

For Linux:

sudo pip install mouse

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

For Windows: Some functions require administrator rights.

Architecture and Working Principles

Main Components

The mouse library consists of several key components:

  • Controller — cursor manipulation and action emulation
  • Listener — mouse event monitoring
  • Recorder — macro recording and playback
  • Event System — event processing infrastructure

Supported Platforms

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

Core Features of the Library

Mouse Action Emulation

mouse works both as a listener that captures user actions and as a controller that emulates movements and clicks.

Key functions:

  • Button press and release tracking
  • Cursor position detection
  • Programmatic clicks and scrolling
  • Cursor movement
  • Mouse hotkeys
  • Macro recording and playback

Event Types

The library processes the following event categories:

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

Cursor Management and 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

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

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

Click Emulation and 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()

Clicking at Specific Coordinates

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

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

Button Hold

# Press and hold
mouse.press('left')
# Perform other actions
mouse.release('left')

# Check button state
if mouse.is_pressed('left'):
    print("Left button is pressed")

Working with the Scroll Wheel

Scroll Emulation

# Scroll up
mouse.wheel(1)

# Scroll down
mouse.wheel(-1)

# Multiple-step scroll
mouse.wheel(-5)  # Scroll down 5 steps

Horizontal Scrolling

# For systems that support horizontal scrolling
mouse.wheel(delta=0, horizontal=True)

Event System and Handlers

Basic Handlers

def click_handler(event):
    print(f"Click: {event.button} at ({event.x}, {event.y})")

# Handle all clicks
mouse.on_click(click_handler)

# Handle double clicks
mouse.on_double_click(lambda e: print("Double click!"))

# Handle right clicks
mouse.on_right_click(lambda e: print("Right click!"))

Advanced Handlers

def button_handler(event):
    if event.event_type == 'down':
        print(f"Button pressed: {event.button}")
    elif event.event_type == 'up':
        print(f"Button released: {event.button}")

# Handle press and release events
mouse.on_button(button_handler, buttons=('left', 'right'), types=('down', 'up'))

Global Handlers

def global_handler(event):
    print(f"Event: {event.event_type}, time: {event.time}")

# Global handler for all events
mouse.hook(global_handler)

# Detach a specific handler
mouse.unhook(global_handler)

# Detach all handlers
mouse.unhook_all()

Macro Recording and Playback

Recording Actions

# Record until right‑button press
print("Recording started. Press the right button to stop.")
events = mouse.record(button='right')

# Time‑limited recording
import time
import threading

def stop_recording():
    time.sleep(10)  # Record for 10 seconds
    return True

events = mouse.record(button='right')

Playing Back Macros

# Playback at normal speed
mouse.play(events)

# Playback with speed adjustment
mouse.play(events, speed_factor=2.0)  # Twice as fast
mouse.play(events, speed_factor=0.5)  # Half speed

# Multiple repetitions
for i in range(3):
    mouse.play(events)
    time.sleep(1)

Waiting for Events

Blocking Wait

# Wait for a left click
print("Waiting for left click...")
mouse.wait('left')
print("Left click detected!")

# Wait for a specific event type
mouse.wait('right', target_types=('down',))  # Press only
mouse.wait('left', target_types=('up',))     # Release only

Non‑Blocking Wait

import threading

def wait_for_click():
    mouse.wait('left')
    print("Click received!")

# Run wait in a separate thread
thread = threading.Thread(target=wait_for_click)
thread.start()

# Main program continues
print("Program is running...")

Complete Method and Function Reference

Method / Function Description Parameters Example Usage
move(x, y, absolute=True, duration=0) Move the cursor x, y – coordinates; absolute – absolute/relative; duration – movement time mouse.move(100, 200)
get_position() Get current coordinates None x, y = mouse.get_position()
click(button='left', coords=None) Perform a click button – which button; coords – target coordinates mouse.click('left', (100, 100))
double_click(button='left') Double click button – which button mouse.double_click()
right_click() Right click None mouse.right_click()
left_click() Left click None mouse.left_click()
middle_click() Middle button click None mouse.middle_click()
press(button='left') Press a button button – which button mouse.press('left')
release(button='left') Release a button button – which button mouse.release('left')
wheel(delta=1) Scroll wheel delta – direction and steps mouse.wheel(-2)
is_pressed(button='left') Check if a button is pressed button – which button if mouse.is_pressed('left'):
on_button(callback, buttons, types) Button event handler callback – function; buttons – list of buttons; types – event types mouse.on_button(handler, ('left',), ('down',))
on_click(callback) Click handler callback – function mouse.on_click(handler)
on_double_click(callback) Double‑click handler callback – function mouse.on_double_click(handler)
on_right_click(callback) Right‑click handler callback – function mouse.on_right_click(handler)
on_middle_click(callback) Middle‑click handler callback – function mouse.on_middle_click(handler)
on_wheel(callback) Wheel event handler callback – function mouse.on_wheel(handler)
wait(button='left', target_types=('down',)) Wait for an event button – which button; target_types – event types mouse.wait('right')
record(button='right') Record events button – stop button events = mouse.record()
play(events, speed_factor=1.0) Play back events events – list of events; speed_factor – playback speed mouse.play(events, 2.0)
hook(callback) Global event handler callback – function mouse.hook(handler)
unhook(callback) Remove a handler callback – function mouse.unhook(handler)
unhook_all() Remove all handlers None mouse.unhook_all()

Practical Usage Examples

Automating Routine Tasks

import mouse
import time

def automate_clicks():
    positions = [(100, 100), (200, 200), (300, 300)]
    
    for pos in positions:
        mouse.move(*pos)
        time.sleep(0.5)
        mouse.click('left')
        time.sleep(0.3)

# Start automation after a 3‑second delay
time.sleep(3)
automate_clicks()

Creating a Game Macro

import mouse
import time

def gaming_macro():
    # Fast click sequence
    for _ in range(10):
        mouse.click('left')
        time.sleep(0.1)
    
    # Move and right‑click
    mouse.move(500, 300)
    mouse.click('right')

# Activate macro on right‑click
mouse.on_right_click(lambda e: gaming_macro())

User Activity Monitoring

import mouse
import time
from datetime import datetime

click_count = 0
last_activity = time.time()

def activity_monitor(event):
    global click_count, last_activity
    click_count += 1
    last_activity = time.time()
    
    print(f"Click #{click_count} at {datetime.now().strftime('%H:%M:%S')}")
    print(f"Position: ({event.x}, {event.y})")

mouse.on_click(activity_monitor)

# Inactivity checker
def check_inactivity():
    while True:
        time.sleep(60)  # Check every minute
        if time.time() - last_activity > 300:  # 5 minutes idle
            print("User has been inactive for over 5 minutes")

import threading
threading.Thread(target=check_inactivity, daemon=True).start()

Integration with Other Libraries

Integration with keyboard

import mouse
import keyboard

def mouse_keyboard_combo():
    # Press Ctrl and click
    keyboard.press('ctrl')
    mouse.click('left')
    keyboard.release('ctrl')

# Trigger combo with hotkey
keyboard.add_hotkey('ctrl+shift+m', mouse_keyboard_combo)

Integration with PyAutoGUI

import mouse
import pyautogui

def advanced_automation():
    # Screenshot before action
    screenshot = pyautogui.screenshot()
    
    # Locate element on screen
    try:
        button_location = pyautogui.locateOnScreen('button.png')
        if button_location:
            center = pyautogui.center(button_location)
            mouse.click('left', center)
    except:
        print("Element not found")

advanced_automation()

Working with Multithreading

import mouse
import threading
import time

def background_clicker():
    while True:
        time.sleep(1)
        current_pos = mouse.get_position()
        if current_pos[0] > 1000:  # Cursor on the right side
            mouse.click('left')

# Run in background thread
clicker_thread = threading.Thread(target=background_clicker, daemon=True)
clicker_thread.start()

# Main program continues
print("Background clicker is active")

Error Handling and Debugging

Exception Management

import mouse
import time

def safe_mouse_operation():
    try:
        mouse.move(100, 100)
        mouse.click('left')
    except Exception as e:
        print(f"Operation error: {e}")
    
def safe_event_handler(event):
    try:
        print(f"Processing event: {event}")
    except Exception as e:
        print(f"Handler error: {e}")

mouse.on_click(safe_event_handler)

Action Logging

import mouse
import logging
from datetime import datetime

# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(message)s')

def log_mouse_events(event):
    logging.info(f"Mouse event: {event.event_type} - {event.button} at ({event.x}, {event.y})")

mouse.hook(log_mouse_events)

Performance Optimization

Minimizing Latency

import mouse
import time

# Fast clicks without delays
def fast_clicking():
    for _ in range(100):
        mouse.click('left')
        # Minimal pause for stability
        time.sleep(0.001)

# Optimized movement
def optimized_movement():
    positions = [(100, 100), (200, 200), (300, 300)]
    
    for pos in positions:
        mouse.move(*pos, duration=0.1)  # Quick move
        mouse.click('left')

Resource Management

import mouse
import atexit

# Automatic cleanup on exit
def cleanup():
    mouse.unhook_all()
    print("All event handlers cleared")

atexit.register(cleanup)

Security and Limitations

Protection Against Infinite Loops

import mouse
import time
import keyboard

def emergency_stop():
    print("Emergency stop activated!")
    mouse.unhook_all()
    exit()

# Emergency stop with ESC
keyboard.add_hotkey('esc', emergency_stop)

def safe_loop():
    max_iterations = 100
    current_iteration = 0
    
    while current_iteration < max_iterations:
        if keyboard.is_pressed('esc'):
            break
        
        mouse.click('left')
        time.sleep(0.1)
        current_iteration += 1

Access Control

import mouse
import getpass

def check_permissions():
    try:
        # Verify mouse control access
        mouse.get_position()
        return True
    except:
        print("Insufficient permissions to control the mouse")
        return False

if check_permissions():
    print("Mouse control access granted")
else:
    print("Run the program with administrator rights")

Cross‑Platform Development

OS‑Specific Considerations

import mouse
import platform

def platform_specific_setup():
    system = platform.system()
    
    if system == "Windows":
        # Windows‑specific setup
        print("Configuring for Windows")
    elif system == "Darwin":  # macOS
        print("Configuring for macOS")
        print("Ensure Accessibility access is granted")
    elif system == "Linux":
        print("Configuring for Linux")
        print("sudo may be required for some operations")

platform_specific_setup()

Comparison with Alternative Solutions

Library Language Mouse Control Event Support Macros Performance Ease of Use
mouse Python Excellent Excellent Excellent High Very high
pynput Python Excellent Excellent Limited Medium High
PyAutoGUI Python Good No Partial Medium High
AutoHotkey Special DSL Excellent Excellent Excellent High Medium
Selenium Python Limited Limited No Low Medium

Frequently Asked Questions

How can I ensure my application is cross‑platform?

The mouse library automatically detects the operating system and uses the appropriate system APIs. Still, you should account for permission differences and test on all target platforms.

Can I use mouse in multithreaded applications?

Yes, mouse is thread‑safe. It’s recommended to run long‑running tasks and event listeners in separate threads to avoid blocking the main thread.

How do I avoid conflicts with antivirus software?

Some antivirus solutions may block programs that use mouse. Add your application to the exclusion list or sign your executables with a digital certificate.

Does mouse impact system performance?

When used properly, the impact is minimal. Avoid generating excessive events or unnecessary handlers, and call unhook_all() to release resources.

How can I test code that uses mouse?

Create virtual display environments or use mock objects to simulate mouse events without affecting the real system.

Can I record a macro and save it to a file?

Yes, events returned by mouse.record() can be serialized with pickle or json for later playback.

import pickle
import mouse

# Record
events = mouse.record()
with open('macro.pkl', 'wb') as f:
    pickle.dump(events, f)

# Playback
with open('macro.pkl', 'rb') as f:
    events = pickle.load(f)
mouse.play(events)

Conclusion

The mouse library is a powerful, versatile tool for programmatic mouse control in Python. Its simple, intuitive API, comprehensive event system, macro capabilities, and cross‑platform support make it ideal for a wide range of automation tasks.

Key advantages of the library include:

  • Minimalist, clear API
  • Support for all primary mouse operations
  • Event system for reactive programming
  • Built‑in macro support
  • Cross‑platform compatibility
  • Active community and regular updates

mouse is suitable both for beginners taking their first steps in automation and for experienced developers building sophisticated RPA, testing, and UI‑control systems.

News