- 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