WinWaitActive not working properly on Alt+Tab Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
panhartstuff
Posts: 25
Joined: 21 Jan 2018, 07:40

WinWaitActive not working properly on Alt+Tab

19 Dec 2018, 12:42

Here's my code:

Code: Select all

GroupAdd, test, ahk_class Notepad
GroupAdd, test, Task Switching ahk_class MultitaskingViewFrame

WinWaitActive ahk_class Notepad
WinWaitNotActive ahk_group test
MsgBox, Test
To replicate my issue, simply run the code, open/activate Notepad, then alt+tab away to any other program other that Notepad.
The expected result should be that the Message Box opens only after you release alt+tab (and thus exiting from the Task Switching menu). But my problem is that the Message Box shows up the instant I hit alt+tab.

The weird thing is that the code works fine without ahk_group:

Code: Select all

WinWaitActive ahk_class Task Switching ahk_class MultitaskingViewFrame
WinWaitNotActive ahk_class Task Switching ahk_class MultitaskingViewFrame
MsgBox, Test
To test the above, simply run the code, alt+tab away to any other program. This one works perfectly as expected, the Message Box will only show up after you release alt+tab.

I'm spinning my head on what I'm really doing wrong, but I just can't figure it out. Any help is truly appreciated!
GEV
Posts: 1002
Joined: 25 Feb 2014, 00:50

Re: WinWaitActive not working properly on Alt+Tab

19 Dec 2018, 13:37

All windowing commands can operate upon a window group by specifying ahk_group MyGroupName for the WinTitle parameter. The commands WinMinimize, WinMaximize, WinRestore, WinHide, WinShow, WinClose, and WinKill will act upon all the group's windows.

By contrast, the other window commands such as WinActivate and IfWinExist will operate only upon the topmost window of the group.
https://autohotkey.com/docs/commands/Gr ... tm#Remarks

In this case

Code: Select all

WinWaitNotActive ahk_group test
operates only upon Notepad (the topmost window of the group),
as far as I can understand.
panhartstuff
Posts: 25
Joined: 21 Jan 2018, 07:40

Re: WinWaitActive not working properly on Alt+Tab

19 Dec 2018, 13:56

I looked through other forum posts, and it doesn't seem to be the case.
From what I gather WinWaitActive ahk_group should wait until any one of the programs in the group is active. While WinWaitNotActive ahk_group should wait until none of the programs in the group are active.

But the more I test the behaviour of ahk_group, the weirder it gets. For example:

Code: Select all

GroupAdd, test, ahk_class Notepad
GroupAdd, test, ahk_class MSPaintApp

WinWaitActive ahk_group test
WinWaitNotActive ahk_group test
MsgBox, Test
The code above should allow you to switch between MSPaint and Notepad for an infinite amount of times without triggering the MsgBox (as long as you don't use the taskbar or alt+tab to switch). In practice, it does work, but only to a certain extent. If you switch back-and-forth between MSPaint and Notepad enough times, the Message Box will randomly get triggered eventually for some reason.
I really can't figure out why this bizzare behavior is happening. Can anyone test if the above happens too, or is my computer just the weird one?
panhartstuff
Posts: 25
Joined: 21 Jan 2018, 07:40

Re: WinWaitActive not working properly on Alt+Tab

19 Dec 2018, 20:55

Sorry for bumping, would really appreciate some help on this
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: WinWaitActive not working properly on Alt+Tab  Topic is solved

19 Dec 2018, 22:38

- I tried this and had some surprise results.
- I just flicked between Notepad and Paint and occasionally I'd get the MsgBox implying that some other window had been active.
- I tried to get info from WinWaitNotActive, via hWnd := WinExist(), in order to get the hWnd and hence info about the unexpected window that had been active, but it didn't work.
- I then tried using a loop as an alternative to WinWaitNotActive, but it appeared that there was no active window, the hWnd was blank.
- Btw I added windows to the group to prevent things like the alt-tab window and Start menu interfering with the test.
- My conclusion is that perhaps WinWaitNotActive and WinGet-ID fail sometimes when the active window is being changed. So you use WinWaitNotActive with ahk_group, you think that it has *succeeded* in finding a window not in the group, when actually it's *failed* to establish what the active window is.
- [EDIT:] If what I said is right then a workaround is to put WinWaitNotActive or WinGet in a loop, and check ErrorLevel to see if an error occurred, break if there was no error, or let the loop resume if there was an error, to try again. Otherwise you could write an alternative version of WinWaitNotActive using WinGet, and if WinGet has an error or returns a blank, then resume the loop, otherwise break.

Code: Select all

q:: ;test switch between Notepad/Paint, are other windows ever active
GroupAdd, test, ahk_class Notepad
GroupAdd, test, ahk_class MSPaintApp
GroupAdd, test, ahk_class TaskSwitcherWnd ;alt-tab
GroupAdd, test, ahk_class DV2ControlHost ;start menu
GroupAdd, test, ahk_class Shell_TrayWnd ;taskbar
GroupAdd, test, ahk_class Button ;start menu

WinWaitActive, ahk_group test
;WinWaitNotActive, ahk_group test

;alternative code for WinWaitNotActive:
;DetectHiddenWindows, Off
DetectHiddenWindows, On
Loop
{
	WinGet, hWnd, ID, A
	if (hWnd = "")
		continue
	WinGet, vPID, PID, % "ahk_id " hWnd
	WinGetClass, vWinClass, % "ahk_id " hWnd
	WinGetTitle, vWinTitle, % "ahk_id " hWnd
	WinGet, vPName, ProcessName, % "ahk_id " hWnd
	if !WinExist("ahk_group test ahk_id " hWnd)
		break
	Sleep, 10
}
;DetectHiddenWindows, On

MsgBox, % Clipboard := hWnd "`r`n" vPID "`r`n" vWinClass "`r`n" vWinTitle "`r`n" vPName
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
panhartstuff
Posts: 25
Joined: 21 Jan 2018, 07:40

Re: WinWaitActive not working properly on Alt+Tab

19 Dec 2018, 23:11

Thank you so much, your code works perfectly!
Your insights really helped clear things up. I guess there's some kind of empty "gap" between switching windows. I also noticed in Window Spy, there's sometimes a small period between switching windows where the Window info box becomes completely empty. I guess that's what trips it up. I wonder if this is worthy of a bug report?
Edit:
Actually I want to ask a bit more

Code: Select all

if !WinExist("ahk_group test ahk_id " hWnd)
Can you clarify what does this really do? I don't quite understand what it is checking
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: WinWaitActive not working properly on Alt+Tab

19 Dec 2018, 23:23

- That's great, glad I could help.
- Re. bug report, I'd say almost. AFAICS WinWaitNotActive does what it says on the tin, so no bugs. But the behaviour could perhaps be better.
- So for a wish list: (a) WinWaitNotActive to set the Last Found Window to be the first active window that does not match the window criteria, or perhaps 0 if there was an error determining the active window.
- (I wouldn't ask for this, but it's worth mentioning, perhaps in addition, to set ErrorLevel to something, e.g. 2 to indicate an error identifying the active window.)

- Re. edit. That line checks for the existence of a window that matches the ahk_group window criteria. The window must also have a specific hWnd. The ! means 'not'.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
panhartstuff
Posts: 25
Joined: 21 Jan 2018, 07:40

Re: WinWaitActive not working properly on Alt+Tab

19 Dec 2018, 23:46

Ok my head isn't really working right now, the script does work, but I still don't understand the logic of how checking the existence of the ahk_group will help check whether or not they're active. To clarify, that line basically checks whether any windows matches with any one of the ahk_class (in this case) listed inside the ahk_group PLUS whether it also matches with the last active handle, is that right?
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: WinWaitActive not working properly on Alt+Tab

20 Dec 2018, 00:53

- These two blocks of code are very similar.
- The first: if the active window is not in the group, break.
- The second: if the hWnd is not in the group, break. We use the hWnd for the active window we'd acquired earlier. (We'd retrieved the hWnd to see if it was blank.)

Code: Select all

	if !WinActive("ahk_group test")
		break
	if !WinExist("ahk_group test ahk_id " hWnd)
		break
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
panhartstuff
Posts: 25
Joined: 21 Jan 2018, 07:40

Re: WinWaitActive not working properly on Alt+Tab

20 Dec 2018, 01:07

Thanks that cleared it up. I didn't know you could use WinExist to compare between a group and a handle like that.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: doodles333, Google [Bot] and 331 guests