OnWin() - call function on window event (WinWaitXXX async)

Post your working scripts, libraries and tools for AHK v1.1 and older
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
Contact:

OnWin() - call function on window event (WinWaitXXX async)

18 Feb 2015, 23:41

OnWin.ahk

Version: 1.1.00.00

License: WTFPL

Requirements: Latest version of AutoHotkey


Installation:
Copy into a function lib folder (for auto-#Inclusion) or use #Include manually. Note: Script must not be copy-pasted into the main script
Syntax: OnWin( event, WinTitle, callback )

Parameter(s):
  • event - Window event to monitor. Valid values are: Exist, Active, Show, Hide, Move, Minimize, Maximize, NotActive/!Active, Close/NotExist/!Exist, CloseAll/NotExistAll/!ExistAll
  • WinTitle - see WinTitle. Due to limitations, ahk_group GroupName is not supported directly. To specify a window group, pass an array of WinTitle(s) instead. OnWin() uses A_TitleMatchMode and A_TitleMatchModeSpeed.
  • callback - Function name, Func object or object. The callback will receive an event object with the ff properties: Event and Window, as its first argument.
About Window Events:
Spoiler
Example Usage:

Code: Select all

#Include <OnWin>
#Persistent
 
OnWin("Exist", "Calculator", Func("C"))
Sleep, 1000
Run, calc.exe
return
 
C(this)
{
	static window
	event := this.Event, window := this.WinTitle
	MsgBox, Event: %event%`nWindow: %window%
	OnWin("Close", window, Func("X"))
	SetTimer, close, -1000
	return
close:
	WinClose, %window%
	return
}
 
X(this)
{
	event := this.Event, window := this.WinTitle
	MsgBox, Event: %event%`nWindow: %window%
	SetTimer, exit, -1 ; allow function to return
	return
exit:
	ExitApp
}
Edit: Changed example code

Credits to Lexikos for demonstrating ObjRegisterActive and jNizM for CreateGUID. I have integrated some parts as needed.

Edit:[list][*]Bumped version to v1.0.03.00, see commit 151bda8 - 02/23/2015
[*]Updated to reflect commit 7878f45 - 02/21/2015
[*]Updated to reflect commit ab9052a - 02/19/2015[/list]
Last edited by Coco on 03 Dec 2015, 08:38, edited 8 times in total.
User avatar
boiler
Posts: 16768
Joined: 21 Dec 2014, 02:44

Re: OnWin() - call function on window event (WinWaitXXX asyn

19 Feb 2015, 01:32

This is going to be very useful. Thanks for sharing it. Is there a limit to how many windows and/or events can be set to be monitored simultaneously?
User avatar
boiler
Posts: 16768
Joined: 21 Dec 2014, 02:44

Re: OnWin() - call function on window event (WinWaitXXX asyn

19 Feb 2015, 01:39

Another question: I don't see Resize as an event. Will a resize of a window without its x,y changing trigger the Move event?
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
Contact:

Re: OnWin() - call function on window event (WinWaitXXX asyn

19 Feb 2015, 01:52

boiler wrote:This is going to be very useful. Thanks for sharing it. Is there a limit to how many windows and/or events can be set to be monitored simultaneously?
It should be able to handle lots of windows events. However, ahk_group is not supported (OP updated regarding this).
boiler wrote:Another question: I don't see Resize as an event. Will a resize of a window without its x,y changing trigger the Move event?
Yes, window sizing is covered my Move
Guest

Re: OnWin() - call function on window event (WinWaitXXX asyn

19 Feb 2015, 02:29

It is a good practice when making a function/class to include version (of your script/class/function) and links to your code as a comment top of the file.

This is going to be useful, thanks.
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
Contact:

Re: OnWin() - call function on window event (WinWaitXXX asyn

19 Feb 2015, 02:50

Guest wrote:It is a good practice when making a function/class to include version (of your script/class/function) and links to your code as a comment top of the file.

This is going to be useful, thanks.
Thanks for the advice. I'm really poor w/ versioning and just rely on the commit. I guess I'll start now. Will make the changes on the next commit. :) OK I've officially version-ed the script + added link to GitHub and forum topic + some minor improvements.
Last edited by Coco on 19 Feb 2015, 04:20, edited 1 time in total.
User avatar
empardopo
Posts: 336
Joined: 06 Oct 2013, 12:50
Location: Spain
Contact:

Re: OnWin() - call function on window event (WinWaitXXX asyn

19 Feb 2015, 03:55

Thanks! More examples are also very useful.
Everything is possible!
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
Contact:

Re: OnWin() - call function on window event (WinWaitXXX asyn

19 Feb 2015, 06:56

Update:
Added support for window groups. Instead of passing ahk_group GroupName, pass an array of WinTitle(s). (e.g.: ["Untitled - Notepad", "Calculator" . . . ]
User avatar
hoppfrosch
Posts: 443
Joined: 07 Oct 2013, 04:05
Location: Rhine-Maine-Area, Hesse, Germany
Contact:

Demo does not work

19 Feb 2015, 09:03

Hmmm... what is supposed to happen in your demo?
Opening notepad - as soon as Notepad window exists I would expect a msgbox to be fired- On closing Notepad a msgbox should be fired as well

... but no msgboxes can be seen here (running most recent example and just having cloned your github-repository) (OnWin v1.0.01.0)

My system:

AutoHotkey : v1.1.19.03-28+gc7055e7 Unicode 32-bit (Installed)
SystemType : x64-based PC
SystemOS : WIN_7 64-bit Service Pack 1 v6.1.7601 (WIN32_NT)
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
Contact:

Re: OnWin() - call function on window event (WinWaitXXX asyn

19 Feb 2015, 09:12

v1.1.19.03-28+gc7055e7, I see that you're running the test build. Just tested and works fine here...Same OS except 32bit. make sure you #Include it and not copy-paste into the main script.
User avatar
hoppfrosch
Posts: 443
Joined: 07 Oct 2013, 04:05
Location: Rhine-Maine-Area, Hesse, Germany
Contact:

Re: OnWin() - call function on window event (WinWaitXXX asyn

19 Feb 2015, 09:26

Coco wrote:v1.1.19.03-28+gc7055e7, I see that you're running the test build. Just tested and works fine here...Same OS except 32bit. make sure you #Include it and not copy-paste into the main script.
Already included it (tried #include <OnWin> as well as #include %A_Scriptdir%/OnWin.ahk) and verified correctly stepping in OnWin-function via debug-output ... (which skows that OnWin is only called for NP_Exist)
... but no Msgboxes are fired.

Will try several variants of AHK (2.0/1.19 Test/Non-Test, Ansi/Unicode, 32/64) ...
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
Contact:

Re: OnWin() - call function on window event (WinWaitXXX asyn

19 Feb 2015, 09:37

I've tested it w/ 1.1.19.03 32-bit(Unicode and ANSI), 1.1.19.03-28+gc7055e7 32-bit(Unicode) and 2.0-a058-fa7ed04 - works fine on all.
User avatar
Relayer
Posts: 160
Joined: 30 Sep 2013, 13:09
Location: Delaware, USA

Re: OnWin() - call function on window event (WinWaitXXX asyn

19 Feb 2015, 10:07

Coco,

I like it because I can replace timers that check for existence of windows periodically. I have a script that runs when an Excel spreadsheet becomes active but I'm finding that your utility does not sense the close of the Excel program unless I set "DetectHiddenWindows" to "Off" in your code. I don't quite understand it largely because I don't understand all of the nuances of hidden windows.

Relayer
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
Contact:

Re: OnWin() - call function on window event (WinWaitXXX asyn

19 Feb 2015, 11:45

Edit: Ok, I found a nuance (for the Close event) when multiple windows match the WinTitle criteria. Apparently, WinWaitClose %WinTitle%(WinTitle specified explicitly) seems to trigger only until the last of the matched windows is closed. An option is to add a way to customize the behavior for Close on whether to behave like WinWaitClose %WinTitle%(WinTitle specified explicitly) OR WinWaitClose(uses the "last found" window, obviously WinExist() should be called prior)

@Relayer, use the Close/NotExist/!Exist event to detect when the window no longer exists(destroyed). Otherwise, if you want to detect if it's just hidden(e.g. minimized to tray, etc.), use the Hide event. You need not change DetectHiddenWindows. Close works for me with Excel using SetTitleMatchMode 2 and specifying Microsoft Excel for WinTitle. Also, make sure that the WinTitle does not change etc., OS version might play a part as well. Try the code below: (Run MS Excel first, then run the code, then close MS Excel)

Code: Select all

#Include <OnWin>

SetTitleMatchMode 2
OnWin("Close", "Microsoft Excel", "X")
return

X(this)
{
	event := this.Event, window := this.Window
	MsgBox Event: %event%`nWindow: %Window%
	SetTimer exit, -1
	return
exit:
	ExitApp
}
I'll be doing some tests with commonly used applications to improve detection... I don't really play with AHK's window commands so it's trial and error on my end as well. :)
User avatar
hoppfrosch
Posts: 443
Joined: 07 Oct 2013, 04:05
Location: Rhine-Maine-Area, Hesse, Germany
Contact:

Re: OnWin() - call function on window event (WinWaitXXX asyn

20 Feb 2015, 00:42

Coco wrote:I've tested it w/ 1.1.19.03 32-bit(Unicode and ANSI), 1.1.19.03-28+gc7055e7 32-bit(Unicode) and 2.0-a058-fa7ed04 - works fine on all.
I've tested with all available current variants of Autohotkey (Test/NonTest, AHK1/AHK2) - all with the same results: the callback NP_EXist() is never called ... :?
amun
Posts: 1
Joined: 20 Feb 2015, 01:14

Re: OnWin() - call function on window event (WinWaitXXX asyn

20 Feb 2015, 01:21

By the demo in German you have to change "notepad" to "editor"
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
Contact:

Re: OnWin() - call function on window event (WinWaitXXX asyn

21 Feb 2015, 06:41

Updated to v1.0.02.00
Added CloseAll/NotExistAll/!ExistAll event. This is mainly geared towards window groups and multiple matching windows. Unlike Close(and its alternative names), with the All suffix, the event will trigger when all matching windows no longer exist. Without the suffix, only the first matching window is required to trigger the event. This behavior is based on WinWaitClose when multiple windows match the WinTitle parameter.
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
Contact:

Re: OnWin() - call function on window event (WinWaitXXX asyn

21 Feb 2015, 07:28

hoppfrosch wrote:I've tested with all available current variants of Autohotkey (Test/NonTest, AHK1/AHK2) - all with the same results: the callback NP_EXist() is never called ...
Please try the updated demo.
amun wrote:By the demo in German you have to change "notepad" to "editor"
I've changed the demo to use Calculator, I' not sure if the window title is the same in German but it should be easier to replace.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: gwarble and 115 guests