Get monitor that mouse is in

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
Drugwash
Posts: 850
Joined: 29 May 2014, 21:07
Location: Ploieşti, Romania
Contact:

Re: Get monitor that mouse is in

15 Nov 2018, 07:41

You're welcome! Image
Part of my AHK work can be found here.
guest3456
Posts: 3454
Joined: 09 Oct 2013, 10:31

Re: Get monitor that mouse is in

16 Nov 2018, 14:34

Maestr0 wrote:
03 Nov 2018, 04:20
Ah, yes, the issue was the hotkey, if you move it down past the last Gui, Show line, it works. I've updated the code in the top post.
based on your indentation of the autoexec, the placement of your Return line, which normally would end the autoexec, is incorrect. the autoexec continues until the first Return statement, OR until the first hotkey. by putting the F12:: hotkey where you have it, the autoexec is implicitely ended, and therefore your Return is unnecessary (and would be in the wrong spot).

should be this:

Code: Select all

	...
	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%
Return

F12:: Reload ; press F12 to show the GUI, to see if it does what we want

MWAGetMonitorMouseIsIn() ; we didn't actually need the "Monitor = 0"
{
	...
}

gibbons6546
Posts: 31
Joined: 07 Apr 2020, 11:53

Re: Get monitor that mouse is in

18 Jul 2020, 19:25

Hi all,

Can anyone help me convert the code from the first post in this thread so that it determines which monitor the active window is on? The code currently determines which monitor the mouse cursor is on.

Thank you for any help as this code is a little bit above my skill level. I've pasted the code below:

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
}
User avatar
Maestr0
Posts: 136
Joined: 05 Dec 2013, 17:43

Re: Get monitor that mouse is in

19 Jul 2020, 04:13

Hah, that's so funny, I was looking at this thread yesterday because I needed the function.
gibbons6546 wrote:
18 Jul 2020, 19:25
Hi all,

Can anyone help me convert the code from the first post in this thread so that it determines which monitor the active window is on? The code currently determines which monitor the mouse cursor is on.

Thank you for any help as this code is a little bit above my skill level. I've pasted the code below:
Anyway, I've re-wrote the script a bit. The script below has two examples, the first one is what you are looking for, I think.

Code: Select all

	#SingleInstance, Force
	#NoEnv

	; example 1: you can supply the function with x and y coordinates of - say - the active window
	WinGetPos, x, y, , , A  ; "A" to get the active window's pos.
	ActiveMon := MWAGetMonitor(x, y) ; you can supply the function with x and y coordinates
	msgbox The active window is on %ActiveMon%
	
	; example 2: do not give any parameters to the function to detect on which monitor the mousecursor is
	ActiveMon := MWAGetMonitor()
	msgbox The mousecursor is on %ActiveMon%
Return

F12::Reload ; press F12 to reload the script

MWAGetMonitor(Mx := "", My := "")
{
	if  (!Mx or !My) 
	{
		; if Mx or My is empty, revert to the mouse cursor placement
		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
}
gibbons6546
Posts: 31
Joined: 07 Apr 2020, 11:53

Re: Get monitor that mouse is in

19 Jul 2020, 11:13

Maestr0 wrote:
19 Jul 2020, 04:13
Hah, that's so funny, I was looking at this thread yesterday because I needed the function.
haha that is quite the coincidence. Thanks for the rewrite Maestr0! Once I get a chance to work it into a script I'm working on (for moving windows between monitors) ill report back if i have any issues.

Thanks again!
User avatar
Drugwash
Posts: 850
Joined: 29 May 2014, 21:07
Location: Ploieşti, Romania
Contact:

Re: Get monitor that mouse is in

19 Jul 2020, 12:18

Maybe the safest way is to get active window's position+size in a RECT using GetWindowRect() and then call MonitorFromRect().
That's because contrary to what is suggested above the latter "retrieves a handle to the display monitor that has the largest area of intersection with a specified rectangle." That is, it doesn't check for where top-left window point is, but for where the largest part of the window lies.
Of course, translation from monitor handle to whatever else remains as an exercise for the user. ;)
Part of my AHK work can be found here.
User avatar
Maestr0
Posts: 136
Joined: 05 Dec 2013, 17:43

Re: Get monitor that mouse is in

19 Jul 2020, 12:22

Drugwash wrote:
19 Jul 2020, 12:18
Maybe the safest way is to get active window's position+size in a RECT using GetWindowRect() and then call MonitorFromRect().
That's because contrary to what is suggested above the latter "retrieves a handle to the display monitor that has the largest area of intersection with a specified rectangle." That is, it doesn't check for where top-left window point is, but for where the largest part of the window lies.
Of course, translation from monitor handle to whatever else remains as an exercise for the user. ;)
Are you saying WinGetPos does not get the top left point of the window?
Or that the script shouldn't use that point?

At any rate, if need be, I can add a few lines that would effectively find the center of the window, and use that as x,y to determine the monitor. At any rate, the function accepts x,y input.
User avatar
Drugwash
Posts: 850
Joined: 29 May 2014, 21:07
Location: Ploieşti, Romania
Contact:

Re: Get monitor that mouse is in

19 Jul 2020, 12:40

I'm saying that, logically, where the window is should be where the most of it lies. So if a window is 98% in monitor 2 but its top-left point is in monitor 1, the result should be 2, not 1. Maybe in some special case one would want the other result, so if a custom function would be built for this purpose it should have an extra parameter to specify whether the user wants the percentile result or the starting-point result.

I wonder what the result would be if the center of the window would fall precisely between a four monitor setup... :)

Oh and we still have an issue to deal with when a window is mostly off-screen regardless of whatever monitor its starting point or rest of it lies.
Part of my AHK work can be found here.
User avatar
Maestr0
Posts: 136
Joined: 05 Dec 2013, 17:43

Re: Get monitor that mouse is in

19 Jul 2020, 13:24

Drugwash wrote:
19 Jul 2020, 12:40
I'm saying that, logically, where the window is should be where the most of it lies. So if a window is 98% in monitor 2 but its top-left point is in monitor 1, the result should be 2, not 1. Maybe in some special case one would want the other result, so if a custom function would be built for this purpose it should have an extra parameter to specify whether the user wants the percentile result or the starting-point result.

I wonder what the result would be if the center of the window would fall precisely between a four monitor setup... :)

Oh and we still have an issue to deal with when a window is mostly off-screen regardless of whatever monitor its starting point or rest of it lies.
think about it, the center of the window is practically always in the monitor the majority of the window is in, unless the window is very very big, or yes, when most of the window is outside of the monitor(s).
User avatar
Drugwash
Posts: 850
Joined: 29 May 2014, 21:07
Location: Ploieşti, Romania
Contact:

Re: Get monitor that mouse is in

19 Jul 2020, 13:38

Yes, we have simple cases and corner cases. As one who's stumbled into corner cases too many times I'd say we should cover them all as best as possible.
Have you tested the APIs I mentioned in such corner cases and compared results to AHK's built-in functions' results?
Part of my AHK work can be found here.
User avatar
Maestr0
Posts: 136
Joined: 05 Dec 2013, 17:43

Re: Get monitor that mouse is in

19 Jul 2020, 13:47

Drugwash wrote:
19 Jul 2020, 13:38
Have you tested the APIs I mentioned in such corner cases and compared results to AHK's built-in functions' results?
No, I've never heard of anyone having the issue you mentioned, nor have I had it myself.
User avatar
Drugwash
Posts: 850
Joined: 29 May 2014, 21:07
Location: Ploieşti, Romania
Contact:

Re: Get monitor that mouse is in

19 Jul 2020, 14:00

I meant the situations you described yourself when window is very big or is mostly outside of display(s). Even if you haven't heard of that or didn't happen to you it doesn't mean it hasn't happened or wouldn't happen in the future. The idea is to cover all the bases in advance. But it's just an idea so... back to my vodka and movie; after all it's my birthday, what am I doing here... :)
Part of my AHK work can be found here.
User avatar
Maestr0
Posts: 136
Joined: 05 Dec 2013, 17:43

Re: Get monitor that mouse is in

19 Jul 2020, 14:06

Drugwash wrote:
19 Jul 2020, 14:00
I meant the situations you described yourself when window is very big or is mostly outside of display(s). Even if you haven't heard of that or didn't happen to you it doesn't mean it hasn't happened or wouldn't happen in the future. The idea is to cover all the bases in advance. But it's just an idea so... back to my vodka and movie; after all it's my birthday, what am I doing here... :)
Congratulations!
User avatar
Drugwash
Posts: 850
Joined: 29 May 2014, 21:07
Location: Ploieşti, Romania
Contact:

Re: Get monitor that mouse is in

19 Jul 2020, 14:07

Thank you very much. All the best to you too! :)
Part of my AHK work can be found here.
Spitzi
Posts: 309
Joined: 24 Feb 2022, 03:45

Re: Get monitor that mouse is in

21 Aug 2022, 15:30

Thanks, @Maestr0, for sharing your code. It was exactly what I needed and I've been trying to do something similar for a long time.
User avatar
Maestr0
Posts: 136
Joined: 05 Dec 2013, 17:43

Re: Get monitor that mouse is in

22 Aug 2022, 10:51

Spitzi wrote:
21 Aug 2022, 15:30
Thanks, @Maestr0, for sharing your code. It was exactly what I needed and I've been trying to do something similar for a long time.
no worries, glad it's of use!

I've found an issue with the code a while ago, and that's to do with scaling. If you have a different scaling of your monitor than 100%, it doesn't work exactly. I've yet to figure that one out (because AHK needs to find the exact scaling to compensate, and I want to automate that). I've tried things like viewtopic.php?style=1&t=95233 but have not been successful. If I find the time I'll tinker with it a bit more :)
Spitzi
Posts: 309
Joined: 24 Feb 2022, 03:45

Re: Get monitor that mouse is in

24 Aug 2022, 02:51

I've found an issue with the code a while ago, and that's to do with scaling. If you have a different scaling of your monitor than 100%, it doesn't work exactly.
have not had a problem so far, I am using a primary monitor set to 100% scaling, and then a big second monitor set to 125% scaling.

Greets

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: KruschenZ and 124 guests