How do I get the mouse dx and dy even when the mouse is locked from Python?

170 views Asked by At

How do I get the mouse dx and dy even when the mouse is locked from Python?

I have been trying to get the deltas of the mouse cursor through python, I can easily get the position however I am trying to train a machine learning model with some video game play that I will be creating, which the mouse' position will be locked by the game.

I have already tried using pynput, and again it works using the listeners however that is only the positions, what I need are the change in mouse position, I am fairly certain it is possible to do, but every search I do comes up with the solution of just grab the mouse position!

1

There are 1 answers

4
David George On

For Mouse position tracking, you can use pyautogui library and it will do the job , it has .position() function , that returns current x , y position for the mouse cursor instantly.

For the deltas we can calculate by storing the previous positions and calculate the difference between the current position and previous to get the deltas , and if you need to get the rate of change ,

you can do it as ( curr_y - prev_y )/( curr_x - prev_x ).

sample code ,

import pyautogui
import time

def track_mouse_position():
    try:

        prev_x, prev_y = pyautogui.position()

        while True:

            curr_x, curr_y = pyautogui.position()

            # Calculate deltas
            delta_x = curr_x - prev_x
            delta_y = curr_y - prev_y
            
            # rate of change
            if delta_x != 0 :
                slope  = delta_y / delta_x 
            else :
                slope  = "undefined"

            # Update previous position
            prev_x, prev_y = curr_x, curr_y

            print(f"Delta X = {delta_x} , Delta Y = {delta_y}")
            print(f"rate of change = {slope}")

            
            time.sleep(0.1)  # Adjust the sleep duration as needed
            
    except KeyboardInterrupt:
        print("Tracking stopped.")

track_mouse_position()