How do I get the coordinates of the mouse relative to the program? #292
-
How do I get the coordinates of the mouse relative to the program? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
In AutoHotkey, the behavior of So, if your program is the active window, there is nothing else you need to do except just call In the wrapper, there is two ways to change this behavior:
As noted in the documentation linked above, you may choose for coord mode to be relative to the If you want the get the mouse coordinates relative to a window that is not active, you can get the mouse position according to the screen, and get the position of the window and then calculate the relative position: def get_mouse_position_relative_to(win) -> tuple[int, int]:
win_pos = win.get_position()
mouse_x, mouse_y = ahk.get_mouse_position(coord_mode='Screen')
relative_position = (mouse_x - win_pos.x, mouse_y - win_pos.y)
return relative_position |
Beta Was this translation helpful? Give feedback.
In AutoHotkey, the behavior of
MouseGetPos
(.mouse_position
or.get_mouse_position()
in theahk
python wrapper) is controlled by the current CoordMode setting. If you do not change the CoordMode, the default behavior is to retrieve the coordinates relative to the currently active window.So, if your program is the active window, there is nothing else you need to do except just call
.mouse_position
or.get_mouse_position()
.In the wrapper, there is two ways to change this behavior:
.set_coord_mode
to change the global CoordMode setting:ahk.set_coord_mode(target='Mouse', relative_to='Screen')
OR
coord_mode
parameter to the.get_mouse_position
method to set the coord mode just…