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

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

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

10 Mar 2015, 02:26

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
		}
ahk_learner

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

18 Mar 2015, 17:56

Bump. (And apologies if I have not explained this clearly. Let me know and I will try to put the problem in different words.)
User avatar
berban
Posts: 97
Joined: 14 Apr 2014, 03:20

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

18 Mar 2015, 20:49

You don't ever check for controls that already exist
Guest

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

18 Mar 2015, 21:18

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!
User avatar
berban
Posts: 97
Joined: 14 Apr 2014, 03:20

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

18 Mar 2015, 23:45

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.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: aitaixy, Google [Bot], Nerafius, RandomBoy and 189 guests