A script to detect when a window opens

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
alancantor
Posts: 76
Joined: 11 Jun 2019, 11:28

A script to detect when a window opens

Post by alancantor » 24 Dec 2020, 14:14

I write custom commands for Dragon (speech recognition software). I'm developing a command that uses an input box to prompt the user for information.

But when the Dragon command is invoked, the inbox box opens but does not receive focus. The cause: uncertain, but my three best guesses are that it's a newly-introduced Dragon bug; that it's a Windows 10 issue; or a combination of the first two. (I notice many more focus-related failures in Windows 10 than in Windows 7.)

Until Dragon's input box problem is sorted, I'm wondering whether I can create a workaround using AHK. Is there a way to use the opening of a window as a macro trigger? What I'm imagining is that the script will activate the window via its title bar text, e.g.,

winactivate, Your name

In Macro Express, the opening of a window, or a window receiving focus, are supported activation methods. I've checked through AHK help searching for something comparable, but no luck!

Curiously, when I tried my workaround with Macro Express, I noted that it's more reliable for Macro Express to watch for a window gaining focus than a window opening. This leads me to suspect that Windows 10 is the culprit.

WatsonEnterprises
Posts: 19
Joined: 25 May 2020, 23:04

Re: A script to detect when a window opens

Post by WatsonEnterprises » 25 Dec 2020, 12:13

You could do something like this. If you search RegisterShellHookWindow or RegisterWindowMessage on the forum, there are some better examples.

Code: Select all

#Persistent
MyShellMessageListener.Register()
return


class MyShellMessageListener extends ShellMessageListener{
	OnShellMessageWindowCreated(wParam, lParam){
		winHWND := lParam
		WinGetTitle, winTitle, % "ahk_id " winHWND
		msgbox % "a new window titled " winTitle " was opened"
		
		;If (winHWND happens to be for a Dragon window)
			;do something
	}
}


class ShellMessageListener{
	static wpHSHELL_WINDOWCREATED := 1, wpHSHELL_WINDOWDESTROYED := 2, wpHSHELL_WINDOWACTIVATED := 4
	
	Register(){
		DllCall("RegisterShellHookWindow", UInt, A_ScriptHwnd )
		MsgNum := DllCall("RegisterWindowMessage", Str, "SHELLHOOK")
		
		funcForShellMessage := this.OnShellMessage.bind(this)
		OnMessage(MsgNum, funcForShellMessage)
	}
	
	OnShellMessage(wParam, lParam){
		switch wParam {
			case this.wpHSHELL_WINDOWCREATED :   this.OnShellMessageWindowCreated(wParam, lParam)
			case this.wpHSHELL_WINDOWDESTROYED : this.OnShellMessageWindowDestroyed(wParam, lParam)
		}
	}
}

scriptor2016
Posts: 864
Joined: 21 Dec 2015, 02:34

Re: A script to detect when a window opens

Post by scriptor2016 » 25 Dec 2020, 15:34

when you speak the command, the GUI containing the input box opens, is this correct?

If so, you could then perhaps use winactivate to activate it - it may be that simple.

So in other words, when you say the word, have it trigger an .ahk script which waits for the GUI with the inputbox to exist (using winwait), and then activate it as mentioned above.

Hopefully something like this might work for you ;)

alancantor
Posts: 76
Joined: 11 Jun 2019, 11:28

Re: A script to detect when a window opens

Post by alancantor » 26 Dec 2020, 13:13

Thank you both for the ideas.

I am wondering whether changing a few Registry settings might also do the trick.

Post Reply

Return to “Ask for Help (v1)”