Valgrind
Valgrind

Reputation: 1

Python: Converting Coordinates for Mouse Movement Across Different Screen Resolutions

I'm currently working on a project where I need to move the mouse cursor to specific coordinates on the screen. To ensure that the mouse movement works across different screen resolutions, I've implemented a function to convert the coordinates before passing them to the 'mouse_move' function.

Here are the functions I'm using:

from pyautogui import *
from win32api import SetCursorPos


def convert_cords(x, y):
    w, h = size()
    original_res = (1360, 768)
    ratio_x = w / original_res[0]
    ratio_y = h / original_res[1]
    new_x = round(x * ratio_x)
    new_y = round(y * ratio_y)
    return (new_x, new_y)

def mouse_move(x, y):
    new_x, new_y = convert_cords(x, y)
    SetCursorPos((new_x, new_y))

The 'convert_cords' function takes the original coordinates (x, y) and scales them based on the ratio of the current screen resolution to the original resolution (1360, 768). The 'mouse_move' function then uses these converted coordinates to move the mouse cursor.

However, when I test the code on a screen resolution of 1920x1080, I notice that the mouse cursor doesn't land exactly where I expect it to. It seems to be off by a few pixels.

I'm wondering if there's a flaw in my approach or if someone has encountered a similar issue and found a solution. Any insights or suggestions would be greatly appreciated. Thank you!

Upvotes: 0

Views: 177

Answers (0)

Related Questions