Page 1 of 1

Wait for new control to exist, ignore older ones w/same name

Posted: 10 Mar 2015, 02:26
by ahk_learner
I'm attempting to have ahk wait for a new control to exist, while making sure that any controls with the same name are ignored at the start of the script. (In other words, ahk should only respond to new instances of the control--which arise only after the script begins.) Below is the code I am currently using. Any help is greatly appreciated.

Code: Select all

^J::
	SetTimer, CtrlWait, 100
	CtrlWait:
		ControlGet, COut, Hwnd, , <<ClassNN>>, <<ahk_class>>
		if !COut
			return
		else
		{
			SetTimer, CtrlWait, Off
		}

Re: Wait for new control to exist, ignore older ones w/same

Posted: 18 Mar 2015, 17:56
by ahk_learner
Bump. (And apologies if I have not explained this clearly. Let me know and I will try to put the problem in different words.)

Re: Wait for new control to exist, ignore older ones w/same

Posted: 18 Mar 2015, 20:49
by berban
You don't ever check for controls that already exist

Re: Wait for new control to exist, ignore older ones w/same

Posted: 18 Mar 2015, 21:18
by Guest
Berban, many thanks for your reply.

I believe I do need to check for controls that already exist because I don't want these particular controls to be triggered later in the script.

For example, say I have a situation where two identical windows of a program are already open, each with an identical set of controls. I would like to run a script that opens a third such window (the window takes a second or two to load) and waits for it to be ready for input. Problem is, if I don't tell AHK to somehow take into account the pre-existing windows when the hotkey is executed, then AHK will think the third window is already open and ready to go.

In practice, I may have between zero and 10 of such windows already open at the time the hotkey is triggered - so hoping the script can dynamically determine the starting number.

Thanks again for any input!

Re: Wait for new control to exist, ignore older ones w/same

Posted: 18 Mar 2015, 23:45
by berban
Sorry for the terse reply last time, I had to run. So you may have some windows already open. Then you might as well check for windows not controls right? In that case I'd use the WinGet, , List http://www.autohotkey.com/docs/commands/WinGet.htm command.

Here's some code to get you started:

Code: Select all

WindowIdentifier = ahk_class Notepad

WinGet, ExistingWindows, List, %WindowIdentifier%
Loop, %ExistingWindows%
	WinList .= ExistingWindows%A_Index% "`n"
SetTimer, CheckNewWindow, 400
Return

CheckNewWindow:
WinGet, CurrentWindows, List, %WindowIdentifier%
Loop %CurrentWindows%
	If !InStr(WinList, CurrentWindows%A_Index% "`n")
	{
		SetTimer, %A_ThisLabel%, Off
		HWND := CurrentWindows%A_Index%
		MsgBox, The window has been found, its HWND is %HWND%
		Break
	}
Return

#Persistent
Just to clarify, (as far as I know) you need to identify windows first before you identify controls. It's like trying to find a house number without knowing the street name first, where the street name is the window. So find the new window with some code like that posted above and then use the %HWND% to find the control you want inside that window.

Re: Wait for new control to exist, ignore older ones w/same   Topic is solved

Posted: 19 Mar 2015, 02:55
by ahk_learner
This does the trick... Works perfectly. Thanks again, berban!