SoFunction
Updated on 2025-03-02

A detailed explanation of how to use Python to output mouse coordinates in real time

Preface

In some application scenarios, we may need to obtain the coordinate information of the mouse on the screen in real time. Python'spynputThe library provides an easy way to implement this functionality. This article will introduce how to use itpynputThe library outputs the mouse coordinates in real time.

Install pynput library

Before you start, make sure you have installed itpynputLibrary. You can use it in the terminal via the following commandpipInstall it:

pip install pynput

Python scripts that output mouse coordinates in real time

from pynput import mouse
import time

def on_move(x, y):
    # Handle mouse movement events and output current coordinates    print(f"Mouse moved to ({x}, {y})")

def on_click(x, y, button, pressed):
    # When the mouse click event occurs, output the click information    if not pressed:
        print(f"Mouse clicked at ({x}, {y}) with {button}")
        # When the mouse click event occurs and is released, exit the listening        return False

def on_scroll(x, y, dx, dy):
    # When the mouse wheel event occurs, output the scroll wheel information    print(f"Mouse scrolled at ({x}, {y}) with delta ({dx}, {dy})")

# Set up the mouse listenerwith (
        on_move=on_move,
        on_click=on_click,
        on_scroll=on_scroll) as listener:
    ()

Code explanation

on_move(x, y):When the mouse moves,This function will be called and output the new coordinates of the mouse (x, y)。
on_click(x, y, button, pressed):When the mouse click event occurs,This function will be called and output the clicked position and button。If click to release(pressed for False),Exit the listener。
on_scroll(x, y, dx, dy):When the mouse wheel event occurs,This function will be called and output the scroll position and the scroll change amount。

Summarize

This article shows how to use Python's pynput library to track and output mouse coordinates in real time. This is a simple and practical example where you can extend or modify the code as needed to suit more complex needs.

Attachment: Capture mouse clicks - don't let go of left and right keys

Let's focus on how to capture mouse click events. Through the class, you can easily listen for left and right click events of the mouse. Here is a simple example showing how to capture these events and print related information:

from pynput import mouse

def on_click(x, y, button, pressed):
    if button == :
        print('Left button clicked at ({0}, {1})'.format(x, y))
    elif button == :
        print('Right button clicked at ({0}, {1})'.format(x, y))

with (on_click=on_click) as listener:
    ()

Run this code, and whenever you click the left or right mouse button, the program will output the clicked position information.

This is the end of this article about using Python to output mouse coordinates in real time. For more related content related to Python to output mouse coordinates in real time, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!