WinWaitActive - speed setting? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
roaldien

WinWaitActive - speed setting?

15 Feb 2015, 06:36

How often does WinWaitActive "refresh" its check? Is there a way to speed it up? I want to handle a popup window but it still flickers visbly on the screen before autohotkey can put input in a control and then press a button to close it. Is there some other way to handle it quicker?
MJs
Posts: 454
Joined: 23 Sep 2014, 03:29

Re: WinWaitActive - speed setting?

15 Feb 2015, 07:12

I don't know about quicker but you can instead of WinWaitActive, use a timer with your own period, or a loop with a sleep period or without it, and use WinActive() or WinExist() for your waiting
that's operating on your own time, yet it's about how you implement it.
User avatar
Nextron
Posts: 1391
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win10 AHK: Unicode x32

Re: WinWaitActive - speed setting?  Topic is solved

15 Feb 2015, 13:43

Instead of polling for the active window and check if it has changed since last time, you can let Windows tell you when a new window is created or the active window changes. This event triggers when the window has been created, but is perhaps not yet visible. The OnMessage causes interruption of running code. So you have a higher chance it's too fast than too slow.

Code: Select all

ShellMessage(wParam, lParam:=""){
    static init:=DllCall("RegisterShellHookWindow", UInt,A_ScriptHwnd)
    ,MsgNum:=OnMessage(DllCall("RegisterWindowMessage", Str,"SHELLHOOK"), "ShellMessage")
    ,cleanup:={base:{__Delete: "ShellMessage"}}

    ;Deregister shell hook
    if !(cleanup)
        return DllCall("DeregisterShellHookWindow","Ptr",A_ScriptHwnd), OnMessage( MsgNum, "" )

    WinGetTitle, WinTitle, ahk_id %lParam%
    WinGetClass, WinClass, ahk_id %lParam%
    
    ;New window created
    If (wParam=1){ ;HSHELL_WINDOWCREATED:=1
        SoundBeep, 2200, 400
        MsgBox, % "Dectected window creation.`n`nTitle:`t`t" WinTitle "`nahk_class:`t`t" WinClass
    
    ;Change of active window 
    }Else If (wParam=4 || wParam=32772){ ;HSHELL_WINDOWACTIVATED:=4 , 32772 == HSHELL_RUDEAPPACTIVATED:=32772 (fullscreen app active)
        SoundBeep, 1200, 200
    }
}

Esc::ExitApp
Try it and listen to the different beeps upon activation or creation, Escape ends the script.
AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

Re: WinWaitActive - speed setting?

12 Dec 2019, 06:14

Nextron wrote:
15 Feb 2015, 13:43
Instead of polling for the active window and check if it has changed since last time, you can let Windows tell you when a new window is created or the active window changes. This event triggers when the window has been created, but is perhaps not yet visible. The OnMessage causes interruption of running code. So you have a higher chance it's too fast than too slow.

Code: Select all

ShellMessage(wParam, lParam:=""){
    static init:=DllCall("RegisterShellHookWindow", UInt,A_ScriptHwnd)
    ,MsgNum:=OnMessage(DllCall("RegisterWindowMessage", Str,"SHELLHOOK"), "ShellMessage")
    ,cleanup:={base:{__Delete: "ShellMessage"}}

    ;Deregister shell hook
    if !(cleanup)
        return DllCall("DeregisterShellHookWindow","Ptr",A_ScriptHwnd), OnMessage( MsgNum, "" )

    WinGetTitle, WinTitle, ahk_id %lParam%
    WinGetClass, WinClass, ahk_id %lParam%
    
    ;New window created
    If (wParam=1){ ;HSHELL_WINDOWCREATED:=1
        SoundBeep, 2200, 400
        MsgBox, % "Dectected window creation.`n`nTitle:`t`t" WinTitle "`nahk_class:`t`t" WinClass
    
    ;Change of active window 
    }Else If (wParam=4 || wParam=32772){ ;HSHELL_WINDOWACTIVATED:=4 , 32772 == HSHELL_RUDEAPPACTIVATED:=32772 (fullscreen app active)
        SoundBeep, 1200, 200
    }
}

Esc::ExitApp
Try it and listen to the different beeps upon activation or creation, Escape ends the script.
Can someone post a sample of using this? I know the title I am expecting (Shipping Schedule) and I know the ahk_class (I do not know the hwnd yet because the window has not been created yet).

So what would I put in the script to wait for this?
User avatar
boiler
Posts: 16962
Joined: 21 Dec 2014, 02:44

Re: WinWaitActive - speed setting?

12 Dec 2019, 11:36

Try the Modification of Nextron's code below. If you have a better method for handling the popup than WinClose, you can replace those lines with your approach. Also, since you only indicated the title, that's all I used to identify the window. You can add the ahk_class as well so you don't end up closing similarly named windows.

Code: Select all

ShellMessage(0)
return

ShellMessage(wParam, lParam:=""){
    static init:=DllCall("RegisterShellHookWindow", UInt,A_ScriptHwnd)
    ,MsgNum:=OnMessage(DllCall("RegisterWindowMessage", Str,"SHELLHOOK"), "ShellMessage")
    ,cleanup:={base:{__Delete: "ShellMessage"}}

    ;Deregister shell hook
    if !(cleanup)
        return DllCall("DeregisterShellHookWindow","Ptr",A_ScriptHwnd), OnMessage( MsgNum, "" )

    WinGetTitle, WinTitle, ahk_id %lParam%
    WinGetClass, WinClass, ahk_id %lParam%
    
	    ;New window created
    If (wParam=1){ ;HSHELL_WINDOWCREATED:=1
        if (WinTitle = "Shipping Schedule")
			WinClose, ahk_id %lParam%
    
    ;Change of active window 
    }Else If (wParam=4 || wParam=32772){ ;HSHELL_WINDOWACTIVATED:=4 , 32772 == HSHELL_RUDEAPPACTIVATED:=32772 (fullscreen app active)
        if (WinTitle = "Shipping Schedule")
			WinClose, ahk_id %lParam%
    }
}

Esc::ExitApp
AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

Re: WinWaitActive - speed setting?

13 Dec 2019, 05:51

boiler wrote:
12 Dec 2019, 11:36
Thank you. I couldn't figure out why it was failing. I looked at winspy at the class of the window I am trying to close and I see it is the same like a save as window! ahk_class #32770 the script maybe does not see this as a new window even when I specified the title
User avatar
boiler
Posts: 16962
Joined: 21 Dec 2014, 02:44

Re: WinWaitActive - speed setting?

13 Dec 2019, 10:36

You might want to try it with the check of the class also, as shown below. That way another window you might have open also named "Shipping Schedule" (could happen!) doesn't get closed.

Code: Select all

ShellMessage(0)
return

ShellMessage(wParam, lParam:=""){
    static init:=DllCall("RegisterShellHookWindow", UInt,A_ScriptHwnd)
    ,MsgNum:=OnMessage(DllCall("RegisterWindowMessage", Str,"SHELLHOOK"), "ShellMessage")
    ,cleanup:={base:{__Delete: "ShellMessage"}}

    ;Deregister shell hook
    if !(cleanup)
        return DllCall("DeregisterShellHookWindow","Ptr",A_ScriptHwnd), OnMessage( MsgNum, "" )

    WinGetTitle, WinTitle, ahk_id %lParam%
    WinGetClass, WinClass, ahk_id %lParam%
    
	    ;New window created
    If (wParam=1){ ;HSHELL_WINDOWCREATED:=1
        if (WinTitle = "Shipping Schedule") && (WinClass = "#32770")
			WinClose, ahk_id %lParam%
    
    ;Change of active window 
    }Else If (wParam=4 || wParam=32772){ ;HSHELL_WINDOWACTIVATED:=4 , 32772 == HSHELL_RUDEAPPACTIVATED:=32772 (fullscreen app active)
        if (WinTitle = "Shipping Schedule") && (WinClass = "#32770")
			WinClose, ahk_id %lParam%
    }
}

Esc::ExitApp

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 362 guests