简体   繁体   中英

Getting cursor position inside of an application window instead of the screen

I'm trying to locate coordinates of my mouse cursor inside of an application window (ex. notepad) but all I can come up with is the position of it on the screen. Is there any way in python to get the xy of the cursor only inside certain window or how to calculate the pos on screen to pos inside an app?? I tried using pyautogui, pyautoit and pywin32 pyautogui.position(), autoit.mouse_get_pos() and win32gui.GetCursorPos()

There is no direct way to achieve that I think.But you could consider it in another way. 在此处输入图像描述

Try code below:

import win32gui

the_window_hwnd = win32gui.GetForegroundWindow()  # hwnd of the specific window, it could be whatever you what
left_top_x, left_top_y, *useless_position = win32gui.GetWindowRect(the_window_hwnd) # get the position of window you gave.

mouse_pos_x, mouse_pos_y = win32gui.GetCursorPos()
pos_in_window_x, pos_in_window_y = (mouse_pos_x - left_top_x), (mouse_pos_y - left_top_y)

print(pos_in_window_x, pos_in_window_y)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM