SoFunction
Updated on 2024-11-20

How to get the real-time mouse position using Python

Using Python's third-party library pyautogui, PyAutoGUI is a pure Python GUI automation tool whose purpose is to automate mouse and keyboard operations with a program, with multi-platform support (Windows, OS X, Linux).

mounting

pip install pyautogui

Sample pyautogui mouse operations

import pyautogui

# Get current screen resolution
screenWidth, screenHeight = ()

# Get current mouse position
currentMouseX, currentMouseY = ()

# 2 seconds of mouse movement at coordinates 100,100 Absolute movement #
#(100, 100,2)
(x=100, y=100,duration=2, tween=)

# Mouse over the center of the screen.
(screenWidth / 2, screenHeight / 2)

# Left mouse click once
#()
# x 
# y 
# clicks Clicks
# interval between clicks
# button 'left', 'middle', 'right' corresponds to left, center, right or value (1, 2, or 3)
# tween function
#
(x=None, y=None, clicks=1, interval=0.0, button='left', duration=0.0, tween=)

# Relative mouse movement , downward movement
#(None, 10)
(xOffset=None, yOffset=10,duration=0.0, tween=)


# Current mouse position 0 interval double click
#()
(x=None, y=None, interval=0.0, button='left', duration=0.0, tween=)

# 3 clicks from the current mouse position
#()
(x=None, y=None, interval=0.0, button='left', duration=0.0, tween=)

#Right click
()

# hit in the middle (of a ball game)
()

# Use the jog/fade function to make the mouse move to the (500,500) position after 2 seconds
#  use tweening/easing function to move mouse over 2 seconds.
(x=500, y=500, duration=2, tween=)

# Mouse Drag and Drop
(x=427, y=535, duration=3,button='left')

#Relative mouse drag
(xOffset=100,yOffset=100,duration=,button='left',mouseDownUp=False)

#Mouse over x=1796, y=778 and press
(x=1796, y=778, button='left')

# Mouse move to x=2745, y=778 and release (selected in combination with mouseDown)
(x=2745, y=778, button='left',duration=5)

# Mouse current position wheel scrolling
()
# Horizontal mouse scrolling (Linux)
()
# Left and right mouse scrolling (Linux)
()

Python to get the real-time position of the mouse concrete implementation

import time
import pyautogui as pag
 
try:
    while True:
        # Get screen resolution
        screenWidth, screenHeight = ()  
        # Get mouse position
        x, y = ()  
        # Print resolution and mouse position
        print("Screen size: (%s %s),  Position : (%s, %s)\n" % (screenWidth, screenHeight, x, y))  
        # Display position at one second intervals
        (1)  
except KeyboardInterrupt:
    print('end')

Results Showcase

The resolution of the screen is 1920×1080, the result after moving the mouse at 1s interval:

summarize

To this article on how to use Python to get the real-time location of the mouse is introduced to this article, more relevant Python to get the real-time location of the mouse content, please search for my previous posts or continue to browse the following related articles I hope that you will support me in the future more!