MouseGetPos

Retrieves the current position of the mouse cursor, and optionally which window and control it is hovering over.

MouseGetPos, [OutputVarX, OutputVarY, OutputVarWin, OutputVarControl, 1]

 

Parameters

OutputVarX/Y

The names of the variables in which to store the X and Y coordinates. The retrieved coordinates are relative to the active window unless CoordMode was used to change to screen coordinates.

OutputVarWin

This optional parameter is the name of the variable in which to store the unique ID number of the window under the mouse cursor. If the window cannot be determined, this variable will be made blank.

The window does not have to be active to be detected. Hidden windows cannot be detected.

OutputVarControl

This optional parameter is the name of the variable in which to store the name of the control under the mouse cursor. If the control cannot be determined, this variable will be made blank.

The names of controls should always match those shown by the version of Window Spy distributed with v1.0.14+ (but not necessarily older versions of Window Spy). However, unlike Window Spy, the window under the mouse cursor does not have to be active for a control to be detected.

1 The number 1 uses a simpler method to determine OutputVarControl. Although this method correctly retrieves the active/topmost child window of an Multiple Document Interface (MDI) application such as SysEdit or TextPad, it may be less accurate for other purposes. [v1.0.25.10+]

 

Remarks

Any of the output variables may be omitted if the corresponding information is not needed.

 

Related

CoordMode, WinGet, SetDefaultMouseSpeed, MouseClick, MouseClickDrag, MouseMove

 

Example

MouseGetPos, xpos, ypos 
Msgbox, The cursor is at X%xpos% Y%ypos%. 

; This example allows you to move the mouse around to see
; the title of the window currently under the cursor:
#Persistent
SetTimer, WatchCursor, 100
return

WatchCursor:
MouseGetPos, , , id, control
WinGetTitle, title, ahk_id %id%
WinGetClass, class, ahk_id %id%
ToolTip, ahk_id %id%`nahk_class %class%`n%title%`nControl: %control%
return