I've found this: https://autohotkey.com/board/topic/9801 ... use-is-in/ but that only works for 2 monitors (and I have 3, as mentioned). The code below works for any monitor setup I've managed to try it on.
In the end, I came up with this script which does what I need:
Code: Select all
#SingleInstance, Force
#NoEnv
; get the mouse coordinates first
Coordmode, Mouse, Screen ; use Screen, so we can compare the coords with the sysget information`
MouseGetPos, Mx, My
information .= "Mx " . Mx . " x My " . My . "`n`n" ; this is just to fill the GUI with some information
ActiveMon := MWAGetMonitorMouseIsIn()
Gui, Add, Text, Border, % "Mx " . Mx . " x My " . My . "`n`nActive Monitor " . ActiveMon
; this next part we need so the script knows the dimensions of the GUI as it's not fixed in dimensions (on purpose)
Gui, Show, Hide
Gui, +LastFound
WinGetPos,,, W, H
SysGet, mwa%ActiveMon%, MonitorWorkArea, %ActiveMon% ; "MonitorWorkArea" will get the desktop space of the monitor EXcluding taskbars
Final_x := max(mwa%ActiveMon%left, min(Mx, mwa%ActiveMon%right - W))
Final_y := max(mwa%ActiveMon%top, min(My, mwa%ActiveMon%bottom - H))
Gui, Show, x%Final_x% y%Final_y%
F12:: Reload ; press F12 to show the GUI, to see if it does what we want
Return
MWAGetMonitorMouseIsIn() ; we didn't actually need the "Monitor = 0"
{
; get the mouse coordinates first
Coordmode, Mouse, Screen ; use Screen, so we can compare the coords with the sysget information`
MouseGetPos, Mx, My
SysGet, MonitorCount, 80 ; monitorcount, so we know how many monitors there are, and the number of loops we need to do
Loop, %MonitorCount%
{
SysGet, mon%A_Index%, Monitor, %A_Index% ; "Monitor" will get the total desktop space of the monitor, including taskbars
if ( Mx >= mon%A_Index%left ) && ( Mx < mon%A_Index%right ) && ( My >= mon%A_Index%top ) && ( My < mon%A_Index%bottom )
{
ActiveMon := A_Index
break
}
}
return ActiveMon
}