Hover mouse to get focus only when selected windows are open

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
milkygirl90
Posts: 565
Joined: 10 Nov 2020, 21:22

Hover mouse to get focus only when selected windows are open

01 Aug 2021, 17:31

I'd like the mouse to automatically activate (focus) the window when hovering over the target, but I only want this to work if two specific applications are open side by side. Is this possible?
Ianizer
Posts: 79
Joined: 07 Mar 2021, 00:06

Re: Hover mouse to get focus only when selected windows are open

01 Aug 2021, 19:08

Try this:

Code: Select all

SetTimer, ScanForWindowChangeUnderMouse, 250
SetTitleMatchMode, 2

ScanForWindowChangeUnderMouse:
	if (WinExist("[WinTitle of First Application Without Brackets]") && WinExist("[WinTitle of Second Application Without Brackets]"))
	{
		WinGet, Application1MinMaxState, MinMax, [WinTitle of First Application Without Brackets]
		WinGet, Application2MinMaxState, MinMax, [WinTitle of Second Application Without Brackets]
		if (Application1MinMaxState = 0 && Application2MinMaxState = 0)
		{
			MouseGetPos,,, CurrentWindowIDUnderMouse
			WinGet, CurrentWindowID, ID, A
			if (CurrentWindowIDUnderMouse != CurrentWindowID)
				WinActivate, ahk_id %CurrentWindowIDUnderMouse%
		}
	}
return
User avatar
milkygirl90
Posts: 565
Joined: 10 Nov 2020, 21:22

Re: Hover mouse to get focus only when selected windows are open

01 Aug 2021, 19:25

does this work with ahk_class / group? I put this in the auto-exe section but it doesn't seem to respond:

Code: Select all

SetTimer, ScanForWindowChangeUnderMouse, 250
SetTitleMatchMode, 2

ScanForWindowChangeUnderMouse:
	if (WinExist("ahk_class ENSingleNoteView") && WinExist("ahk_group MyBrowsers"))
	{
		WinGet, Application1MinMaxState, MinMax, [WinTitle of First Application Without Brackets]
		WinGet, Application2MinMaxState, MinMax, [WinTitle of Second Application Without Brackets]
		if (Application1MinMaxState = 0 && Application2MinMaxState = 0)
		{
			MouseGetPos,,, CurrentWindowIDUnderMouse
			WinGet, CurrentWindowID, ID, A
			if (CurrentWindowIDUnderMouse != CurrentWindowID)
				WinActivate, ahk_id %CurrentWindowIDUnderMouse%
		}
	}
User avatar
Hellbent
Posts: 2102
Joined: 23 Sep 2017, 13:34

Re: Hover mouse to get focus only when selected windows are open

01 Aug 2021, 19:32

milkygirl90 wrote:
01 Aug 2021, 19:25
does this work with ahk_class / group? I put this in the auto-exe section but it doesn't seem to respond:

Code: Select all

SetTimer, ScanForWindowChangeUnderMouse, 250
SetTitleMatchMode, 2

ScanForWindowChangeUnderMouse:
	if (WinExist("ahk_class ENSingleNoteView") && WinExist("ahk_group MyBrowsers"))
	{
		WinGet, Application1MinMaxState, MinMax, [WinTitle of First Application Without Brackets]
		WinGet, Application2MinMaxState, MinMax, [WinTitle of Second Application Without Brackets]
		if (Application1MinMaxState = 0 && Application2MinMaxState = 0)
		{
			MouseGetPos,,, CurrentWindowIDUnderMouse
			WinGet, CurrentWindowID, ID, A
			if (CurrentWindowIDUnderMouse != CurrentWindowID)
				WinActivate, ahk_id %CurrentWindowIDUnderMouse%
		}
	}
You would need to add #Persistent for that code to work.
User avatar
milkygirl90
Posts: 565
Joined: 10 Nov 2020, 21:22

Re: Hover mouse to get focus only when selected windows are open

01 Aug 2021, 21:14

yes I have that in my script too, but no dice too.
Ianizer
Posts: 79
Joined: 07 Mar 2021, 00:06

Re: Hover mouse to get focus only when selected windows are open

01 Aug 2021, 21:47

@milkygirl90
I think you didn't see lines 7 and 8 needed the WinTitles you chose, but here's an improved version of the script (which should work as-is):

Code: Select all

SetTimer, ScanForWindowChangeUnderMouse, 250
;SetTitleMatchMode, 2 ;this usually isn't necessary, i just forgot to remove it from when I tested the script ;I also tested this in a script that contained hotkeys, so I had no idea #Persistent was needed.

ScanForWindowChangeUnderMouse:
	WinGet, Application1MinMaxState, MinMax, ahk_class ENSingleNoteView
	WinGet, Application2MinMaxState, MinMax, ahk_group MyBrowsers
	if (Application1MinMaxState = 0 && Application2MinMaxState = 0)
	{
		MouseGetPos,,, CurrentWindowIDUnderMouse
		WinGet, CurrentWindowID, ID, A
		if (CurrentWindowIDUnderMouse != CurrentWindowID)
			WinActivate, ahk_id %CurrentWindowIDUnderMouse%
	}
User avatar
milkygirl90
Posts: 565
Joined: 10 Nov 2020, 21:22

Re: Hover mouse to get focus only when selected windows are open

01 Aug 2021, 23:55

this presents a number of issues:
1. window gets hidden if I hover to my taskbar
2. the script is active regardless of whether both windows are open in front of me.
Ianizer
Posts: 79
Joined: 07 Mar 2021, 00:06

Re: Hover mouse to get focus only when selected windows are open

02 Aug 2021, 12:16

If this is a standalone script, try this, but make sure to read the comments:

Code: Select all

#Persistent
;To make things simple, put your application names below
Application1 := "WinTitle 1 in quotes"
Application2 := "WinTitle 2 in quotes"

SetTimer, ScanForWindowChangeUnderMouse, 250

ScanForWindowChangeUnderMouse:
	if (!WinExist(Application1) || !WinExist(Application2)) ;if one of the programs don't exist, exit the script.
		ExitApp
	WinGet, Application1MinMaxState, MinMax, %Application1%
	WinGet, Application2MinMaxState, MinMax, %Application2%
	;un-comment the below 2 lines if you want the script to exit when the two applications aren't side-by-side. also comment out or delete the first 2 lines of this subroutine if you do.
	;if (Application1MinMaxState != 0 || Application2MinMaxState != 0) ;if one of the programs is maximized or minimized, exit the script.
		;ExitApp
	WinGet, Application1ID, ID, %Application1%
	WinGet, Application2ID, ID, %Application2%
	MouseGetPos,,, CurrentWindowIDUnderMouse
	;WinGet, CurrentActiveWindowID, ID, A ;this isn't needed, but if things start acting weird, add it back and add "CurrentActiveWindowID != CurrentWindowIDUnderMouse && " to the beginning of the if () below without quotes.
	if (Application1MinMaxState = 0 && Application2MinMaxState = 0 && (CurrentWindowIDUnderMouse = Application1ID || CurrentWindowIDUnderMouse = Application2ID))
		WinActivate, ahk_id %CurrentWindowIDUnderMouse%
return
If it's not a standalone script, the only way I know how to disable and enable the timer is a hotkey:

Code: Select all

;To make things simple, put your application names below
Application1 := "WinTitle 1 in quotes"
Application2 := "WinTitle 2 in quotes"
SetTimer, ScanForWindowChangeUnderMouse, 250

#If (WinExist(Application1) && WinExist(Application2))
key of your choice::SetTimer, ScanForWindowChangeUnderMouse, 250 ;replace "key of your choice" with a key of your choice. (lol).
#If

ScanForWindowChangeUnderMouse:
	if (!WinExist(Application1) || !WinExist(Application2)) ;if one of the programs don't exist, turn off the timer.
		SetTimer, ScanForWindowChangeUnderMouse, Off
	WinGet, Application1MinMaxState, MinMax, %Application1%
	WinGet, Application2MinMaxState, MinMax, %Application2%
	;un-comment the below 2 lines if you want the timer to stop when the two applications aren't side-by-side. also comment out or delete the first 2 lines of this sub if you do.
	;if (Application1MinMaxState != 0 || Application2MinMaxState != 0) ;if one of the programs is maximized or minimized, turn off the timer
		;SetTimer, ScanForWindowChangeUnderMouse, Off
	WinGet, Application1ID, ID, %Application1%
	WinGet, Application2ID, ID, %Application2%
	MouseGetPos,,, CurrentWindowIDUnderMouse
	;WinGet, CurrentActiveWindowID, ID, A ;this isn't needed, but if things start acting weird, add it back and add "CurrentActiveWindowID != CurrentWindowIDUnderMouse && " to the beginning of the if () below without quotes.
	if (Application1MinMaxState = 0 && Application2MinMaxState = 0 && (CurrentWindowIDUnderMouse = Application1ID || CurrentWindowIDUnderMouse = Application2ID))
		WinActivate, ahk_id %CurrentWindowIDUnderMouse%
return
wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: Hover mouse to get focus only when selected windows are open

03 Aug 2021, 10:03

It works very well. Congratulations Ianizer :thumbup:

but why doesn't it work if one of the applications is on a second monitor?

Any solutions?
Ianizer
Posts: 79
Joined: 07 Mar 2021, 00:06

Re: Hover mouse to get focus only when selected windows are open

03 Aug 2021, 13:05

Hmm, I'm not sure. Run this script (and follow the instructions from the MsgBoxes) and tell me if anything doesn't work as it should (and make sure the windows are neither maximized nor minimized). That will tell me what isn't working in the activate-window-under-mouse script.

Code: Select all

SetBatchLines, -1
;as with the other script, place your application names below. Then you can run it.
Window1 := "WinTitle1 in quotes"
Window2 := "WinTitle2 in quotes"

MsgBox, First we check if WinExist() works with two monitors. Move Window2 to the second monitor, leaving Window1 in the first monitor. Don't close this MsgBox until you're done.
MsgBox, % "Window1 exists: " ((WinExist(Window1)) ? ("Yes.") : ("No.")) "`nWindow2 exists: " ((WinExist(Window2)) ? ("Yes.") : ("No.")) "`nNeither should say ""No"""

MsgBox, Next, we test WinActivate. After closing this MsgBox, Window1 should be activated, then 1 second later, Window2 on the second monitor should be activated.
WinActivate, %Window1%
Sleep, 1000
WinActivate, %Window2%
Sleep, 1000

MsgBox, Take note if either window wasn't activated. Next, we'll try getting both window's MinMax state.
WinGet, Window1MinMaxState, MinMax, %Window1%
WinGet, Window2MinMaxState, MinMax, %Window2%
MsgBox, Move both windows to the same monitor (before clicking OK).
WinGet, NewWindow1MinMaxState, MinMax, %Window1%
WinGet, NewWindow2MinMaxState, MinMax, %Window2%
MsgBox, All values should be 0:`nValue1: %Window1MinMaxState%`nValue2: %NewWindow1MinMaxState%`nValue3: %NewWindow2MinMaxState%`nValue4: %NewWindow2MinMaxState%

MsgBox, Lastly, the IDs will be tested. Keep both windows on the same monitor until told otherwise.
WinGet, ID1, ID, %Window1%
WinGet, ID2, ID, %Window2%
MsgBox, Now move Window2 to the other monitor (before clicking OK).
WinGet, NewID1, ID, %Window1%
WinGet, NewID2, ID, %Window2%
MsgBox, Current IDs:`nWindow1: %NewID1%`nWindow2: %NewID2%`nIDs before moving Window2:`nWindow1: %ID1%`nWindow2: %ID2%.`n Window1's IDs should be the same, likewise with Window2. If they are somehow different, then it should be an easy fix. Tell me if anything didn't work as it should have.
ExitApp
I would have tested this myself, but I don't have a second monitor port. Sorry.
wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: Hover mouse to get focus only when selected windows are open

03 Aug 2021, 15:07

Sorry Ianizer, it does work. :P But applications don't have to be maximized. Was it what you were looking for?

It's too late for me today, tomorrow I'll run the last script you've written and tell you the results. (I've opened it and I get a lot of windows, tomorrow I'll analyze what your script does).
Ianizer
Posts: 79
Joined: 07 Mar 2021, 00:06

Re: Hover mouse to get focus only when selected windows are open

03 Aug 2021, 20:13

Oh, ok cool! Well, there's no longer a need to run the newest script I posted. It was just a testing script that, well, pretty much does nothing. It would simply show if there are problems using WinGet and WinExist() with two monitors.
wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: Hover mouse to get focus only when selected windows are open

04 Aug 2021, 13:46

I still ran it, since you went to the trouble, Ianizer. Your scripts are very instructive.

The first value I got:
Window1 exists: Yes.
Window2 exists: Yes.
Neither should say "No"

The second value I got:
All values should be 0:
Value1: 0
Value2: 0
Value3: 0
Value4: 0

The third value I got:
Current IDs:
Windowl: Ox10a00
Window2: Ox5057e
IDs before moving Window2:
Windowl: Ox10a00
Window2: Ox5057e.
Windowl's IDs should be the same, likewise with Window2. If they are somehow different, then it should be an easy fix. Tell me if anything didn't work as it should have.

¡¡All good!! :thumbup:

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], Rohwedder, william_ahk and 174 guests