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

Post your working scripts, libraries and tools
User avatar
RobertL
Posts: 540
Joined: 18 Jan 2014, 01:14
Location: China

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

22 Sep 2015, 19:46

@Coco Nice, thank you.

@lexikos No, I mean any window as script can get such as using WinWaitActive.
Eh...Means multi parallel tasks, that won't filling Windows Task Manager as many as the task count. :(
Coco wrote:only one child process is used so it's somehow more "friendly" to Windows Task Manager
我为人人,人人为己?
User avatar
evilC
Posts: 4787
Joined: 27 Feb 2014, 12:30

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

27 Oct 2015, 10:31

FYI, The demo script does not appear to work with v1.1.00.00-alpha. The callbacks never get fired.

v1.0.03.00 seems fine.

The link at the top of the OP points to v1.1.00.00-alpha, but the post says v1.0.03.00

Tested with AHK 1.1.22.07 (u64)

[Edit] Hmm, it maybe is because the script exits before the callback gets fired? Adding #persistent to my test script seemed to fix it.
titep
Posts: 31
Joined: 14 Nov 2015, 09:01

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

03 Dec 2015, 07:14

Why the sample doesn't work in my Win10? @@
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
GitHub: cocobelgica

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

03 Dec 2015, 07:48

Edit: Just discovered a bug, let me fix it first -> Fixed
titep wrote:Why the sample doesn't work in my Win10? @@
My bad, the example in the first post is not up to date, OnWin requires a function object, so it should be OnWin("Exist", "Calculator", Func("C")). I'll update the original post. -> OP updated, example code should work now.
titep
Posts: 31
Joined: 14 Nov 2015, 09:01

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

03 Dec 2015, 08:56

It works now Coco. Thank you for your prompt response!
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
GitHub: cocobelgica

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

03 Dec 2015, 09:03

Great :)
JJohnston2
Posts: 193
Joined: 24 Jun 2015, 23:38

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

15 Jan 2016, 01:41

I like the idea of this code, but when I tried to use it (and tried the example code), it would only work once for an event, and then that event wouldn't trigger any more. It did this in my functions as well as for the example script.

If I set a timer with a long enough delay (say 1000ms) to re-call the OnWin(...) function I was trying to use, then it would work to grab subsequent events.

If I tried to re-call the OnWin(...) function inside of the function itself that was handling the return call, then it would just automatically re-trigger itself indefinitely. For example, when I tried to trigger a function on a Minimize event for a particular window, it would only catch the first Minimize. If I tried to re-call the OnWin(...) statement inside my handler function, it would immediately re-trigger indefinitely, even though obviously only one Minimize event on a particular window took place to kick off the chain of events. And then if I used a SetTimer it would re-execute correctly if the delay was long enough to give the original handler time to exit (I think that's what was happening).

None of this seems right, but I wasn't sure how to dig into OnWin function code to see why it was getting disabled, or why it wasn't getting retriggered. If this is not normal behavior I'd be interested in trying to figure out what I'm doing to break it, or if it is something related to my script setup. The code I was using was from the original post I believe at the top of this thread.
gbm

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

28 Jan 2016, 10:33

JJohnston2 wrote:I like the idea of this code, but when I tried to use it (and tried the example code), it would only work once for an event, and then that event wouldn't trigger any more. It did this in my functions as well as for the example script.

If I set a timer with a long enough delay (say 1000ms) to re-call the OnWin(...) function I was trying to use, then it would work to grab subsequent events.

If I tried to re-call the OnWin(...) function inside of the function itself that was handling the return call, then it would just automatically re-trigger itself indefinitely. For example, when I tried to trigger a function on a Minimize event for a particular window, it would only catch the first Minimize. If I tried to re-call the OnWin(...) statement inside my handler function, it would immediately re-trigger indefinitely, even though obviously only one Minimize event on a particular window took place to kick off the chain of events. And then if I used a SetTimer it would re-execute correctly if the delay was long enough to give the original handler time to exit (I think that's what was happening).

None of this seems right, but I wasn't sure how to dig into OnWin function code to see why it was getting disabled, or why it wasn't getting retriggered. If this is not normal behavior I'd be interested in trying to figure out what I'm doing to break it, or if it is something related to my script setup. The code I was using was from the original post I believe at the top of this thread.
I'm having the same issue, I just want a little script to sit there and set certain windows to AlwaysOnTop but it only works the first time.
JJohnston2
Posts: 193
Joined: 24 Jun 2015, 23:38

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

31 Jan 2016, 18:31

gbm wrote:I'm having the same issue, I just want a little script to sit there and set certain windows to AlwaysOnTop but it only works the first time.
OnWin() is not necessarily the best or only way to tackle the problem. One of the means for doing what you mention is just to write a loop with WinWaitNotActive and then check if your desired window is active, set it AlwaysOnTop if so, and then continue the loop.

Another means would be to adapt a slightly more functional framework to do basically the same thing... see this post for example, which has code that can be adapted to meet your needs.
gbm

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

01 Feb 2016, 09:38

I was able to accomplish it using a loop and WinWaitActive.
It seems strange that what looks like an event driven function (OnWin) would only work for the first event. Kind of defeats the purpose.
tmplinshi
Posts: 1599
Joined: 01 Oct 2013, 14:57

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

14 Apr 2016, 04:28

Nice! But unfortunately it doesn't work for compiled exe.
sancarn
Posts: 224
Joined: 01 Mar 2016, 14:52

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

09 Jan 2017, 08:31

gbm wrote:I was able to accomplish it using a loop and WinWaitActive.
It seems strange that what looks like an event driven function (OnWin) would only work for the first event. Kind of defeats the purpose.
Probably a bit late, but use Exit at the end of your function. E.G.

Code: Select all

OnWin("Exist","Notepad",Func("Callback_Event"))

Callback_Event(this){
    ;Do stuff
    s:=this.event
    msgbox,%s%
    Exit
}
Something that I've been struggling with is using OnWin() to target the resize event of a 'FileSelectFile' dialog... I.E.

Code: Select all

OnWin("Move","Open - (64-Bit) ahk_class #32770 ahk_exe AutoHotkey.exe",Func("Callback_DLGResize"))
Doesn't seem to call the DLG Resize event at all... (P.S. I am calling from a seperate AHK script, if that makes a difference.
eggbrow
Posts: 2
Joined: 10 Apr 2016, 10:56

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

06 Feb 2017, 22:55

When I try opening a script I made:

Code: Select all

#NoEnv
#IfWinActive Old School RuneScape
#Include <OnWin>
#Include %A_Documents%\AutoHotkey\Lib\OnWin.ahk
#Include C:\Program Files\AutoHotkey\Lib\OnWin.ahk
#Persistent
OnWin("Close", "Old School RuneScape",  Func("C"))
MsgBox, 4, My title, One does not simply close 

OldSchool RuneScape. Are you sure you want to close?
I get this:
Image
Image
Have played around with the Ptr function a bit and still can't get anywhere.

Any help?
gongnl
Posts: 72
Joined: 05 Jan 2015, 03:57
Location: /gongnltmp/

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

20 May 2017, 08:34

when I compile the Example code to .exe file, it doesn't work. why ?Anyone help me? thanks

Return to “Scripts and Functions”

Who is online

Users browsing this forum: pgeugene and 20 guests