- 1 year ago
Determining which monitor the cursor is in via AutoHotKey. Monitor bottom value is wrong
To determine which monitor the cursor is in using AutoHotKey, you can utilize the MonitorFromPoint
function from the Windows API. However, the MonitorFromPoint
function alone may not give you accurate results for the bottom value of the monitor.
Here's an example script that demonstrates how you can determine the monitor the cursor is in and retrieve its dimensions:
autohotkeyCoordMode, Mouse, Screen
MouseGetPos, xPos, yPos
hMonitor := DllCall("MonitorFromPoint", "Int", xPos, "Int", yPos, "UInt", 2)
monitorInfo := { cbSize: 40 }
DllCall("GetMonitorInfo", "Ptr", hMonitor, "Ptr", &monitorInfo)
monitorLeft := monitorInfo.rcMonitorLeft
monitorTop := monitorInfo.rcMonitorTop
monitorRight := monitorInfo.rcMonitorRight
monitorBottom := monitorInfo.rcMonitorBottom
MsgBox Monitor Dimensions:`nLeft: %monitorLeft%`nTop: %monitorTop%`nRight: %monitorRight%`nBottom: %monitorBottom%
The script uses the MonitorFromPoint
and GetMonitorInfo
functions from the Windows API to retrieve the monitor information based on the cursor's position. The monitor dimensions (left, top, right, bottom) are then displayed in a message box.
Note that the MonitorFromPoint
function returns an HMONITOR
handle, and the GetMonitorInfo
function requires a MONITORINFO
structure to be passed as a pointer.
Please make sure you have the latest version of AutoHotKey and run the script as administrator to ensure proper access to the Windows API functions.