Page 1 of 1

IfWinExist can't find a window on a different desktop on Windows 10

Posted: 24 Nov 2019, 19:36
by True Falcon
[Moderator's note: Topic moved from Bug Reports.]

I toggle several programs on or off screen using code like this:

Code: Select all

TogglemIRC:
CapsLock::
	SetTitleMatchMode 2
	IfWinExist %mIRCID%
	{
		IfWinActive %mIRCID%
		{
			WinMinimize %mIRCID%
		}
		Else
		{
			WinActivate %mIRCID%
		}
		Return
	}
	Run C:\Program Files (x86)\mIRC\mirc.exe
	Return
I updated AHK to 1.1.32.00 today and suddenly this does not work if I'm on a different desktop than where mIRC is currently running. Today it starts a new instance of mIRC using my alternate nick (since the primary is in use) on the current desktop. Previously it would switch to the desktop where mIRC was already running and bring it to the front. Since the code is starting a new instance, it the IfWinExist that is failing for me, but I suspect all the "IfWin..." commands will now fail to find an open window on a different desktop.

I downgraded to 1.1.31.00 and now the above code works as intended.

Re: IfWinExist can't find a window on a different desktop on Windows 10

Posted: 24 Nov 2019, 19:53
by gregster
Welcome to the forum!

Probably not a bug, but intended behaviour in version 1.1.32.00.

I assume that the target windows are actually "cloaked". The change log entry for 1.1.32.00 mentions a related change that was made for consistency reasons, I believe:
https://www.autohotkey.com/docs/AHKL_ChangeLog.htm#v1.1.32.00 wrote:Changed commands and functions with a WinTitle parameter to treat cloaked windows as hidden.
Also, the docs explicitly mention that windows on non-active desktops (on Win10) could be affected by this change:
https://www.autohotkey.com/docs/commands/DetectHiddenWindows.htm#Remarks wrote:[v1.1.32+]: Cloaked windows are also considered hidden. This includes suspended Universal Windows Platform apps and windows which are on a non-active virtual desktop (on Windows 10). Such windows are hidden from view, but might still have the WS_VISIBLE window style. Prior to v1.1.32, all windows with the WS_VISIBLE style were considered visible.
(red text color added by me)

You could probably fix this by adding DetectHiddenWindows, On (but look out for side-effects).

Re: IfWinExist can't find a window on a different desktop on Windows 10

Posted: 24 Nov 2019, 20:51
by True Falcon
Thanks, gregster!

That restored the previous behaviour. I just set the default to On for now. I have no reason at this point to cloak windows, but now, if I ever need to, I'll know how to do it!

😎🚲

Re: IfWinExist can't find a window on a different desktop on Windows 10

Posted: 24 Nov 2019, 21:06
by gregster
Thanks for the feedback!
If I understand the concept of cloaked windows correctly, this problem was rather about detecting cloaked windows than cloaking them yourself.

My understanding is that the "cloaking" is something that might occur on Windows 10, due to broader changes like the introduction of virtual desktops and UWP apps. The question is just how to handle these cloaked windows (which might still have the WS_VISIBLE style) - and that handling was changed by Lexikos in the newest version, so that only actually visible windows on the current desktop are really considered visible by AHK. But I don't know much about cloaked windows myself, so far...

Re: IfWinExist can't find a window on a different desktop on Windows 10

Posted: 03 Jan 2020, 14:31
by kartunes
I came here to post this bug. For me, adding DetectHiddenWindows, On does not fix the problem for:

Code: Select all

 ;; Firefox
        #!f::
        if WinExist("ahk_exe firefox.exe")
            WinActivate
         else
            Run "D:\PortableApps(NS)\apps\firefox\current\firefox.exe" 
        Return
When DetectHiddenWindows, On is added at the start of the script, nothing happens after pressing #!f. That is the firefox window is not activated nor is a new instance started.

I have rolled back to the prior version when things worked. Hopefully this gets worked out.

Re: IfWinExist can't find a window on a different desktop on Windows 10

Posted: 10 Dec 2020, 11:14
by mshafer1
kartunes wrote: ↑
03 Jan 2020, 14:31
I came here to post this bug. For me, adding DetectHiddenWindows, On does not fix the problem for:

;; Firefox
#!f::
if WinExist("ahk_exe firefox.exe")
WinActivate
else
Run "D:\PortableApps(NS)\apps\firefox\current\firefox.exe"
Return

When DetectHiddenWindows, On is added at the start of the script, nothing happens after pressing #!f. That is the firefox window is not activated nor is a new instance started.

I have rolled back to the prior version when things worked. Hopefully this gets worked out.
I've found that adding a WinShow (before the WinActivate) makes similar code work for me, but causes undesired behavior with apps like Micorosoft Teams or apps that use a Chrome browser as a hidden window that (as now hidden windows are found and made visble rather than the main app which is visible but cloaked).

Would be nice if we could get a `DetectCloakedWindows`...

Re: IfWinExist can't find a window on a different desktop on Windows 10

Posted: 03 Mar 2022, 10:40
by scatrmind
I have a similar issue despite "DetectHiddenWindows, On" at the beginning of the script. I am trying to detect other running AHK files. WinExist() does not always work. WinExist(WinTitle) has the highest failure rate. WinExist("ahk_id" hwnd) seems to have greater consistency but is not 100%. Getting the hwnd from WinGet and WinExist() does not usually work. I am having the other AHK files write their A_ScriptHwnd to the registry (separate registry key for each AHK file). Then I read the registry keys so that I have the appropriate hwnd's just before passing to WinExist(). This works MOST of the time, but as I said, not EVERY time. When passing "ahk_id " hwnd to WinExist(), WinExist() should just be performing the IsWindow DLL call under the hood before taking action, but A_ScriptHwnd isn't always returning a value. I think far more people are having issues detecting hidden windows; they just aren't speaking up.