[alpha] WinEvent - easily detect window open, close, move, and more

Post your working scripts, libraries and tools.
Descolada
Posts: 1187
Joined: 23 Dec 2021, 02:30

Re: [alpha] WinEvent - easily detect window open, close, move, and more

Post by Descolada » 15 May 2024, 13:38

@Archimede I've updated the library as follows:
1) Added WinEvent.Exist, which activates on either window creation, showing, or name change, and not activated again until the same window doesn't match WinTitle criteria any longer
2) WinEvent.Restore also activates on restoring from maximized state
3) Events follow DetectHiddenWindows and DetectHiddenText settings, with the exception of Create and Show because these usually require DetectHiddenWindows enabled
4) Updated comments a little bit

Archimede
Posts: 550
Joined: 25 Nov 2021, 09:49
Location: Switzerland / Italy

Re: [alpha] WinEvent - easily detect window open, close, move, and more

Post by Archimede » 16 May 2024, 08:16

@Descolada
Hallo.
Nice!
I tested it, seem work well.
I no understood the third point:
3) Events follow DetectHiddenWindows and DetectHiddenText settings, with the exception of Create and Show because these usually require DetectHiddenWindows enabled
please can explain it me better?
Remain the problem about
SetTitleMatchMode( ... )

Descolada
Posts: 1187
Joined: 23 Dec 2021, 02:30

Re: [alpha] WinEvent - easily detect window open, close, move, and more

Post by Descolada » 16 May 2024, 09:00

@Archimede what exactly remains the problem with SetTitleMatchMode. It works as expected for me:

Code: Select all

#include WinEvent.ahk
#Requires AutoHotkey v2 

SetTitleMatchMode 3
WinEvent.Show((*) => ToolTip("MatchMode 3", 100, 100, 1), "Notepad ahk_exe notepad.exe")
SetTitleMatchMode 2
Persistent()
When Notepad is ran then no tooltip is shown, even though TitleMatchMode is 2.

Previously DetectHiddenWindows was always On, which meant that events like WinEvent.Move could have detected hidden windows moving as well, which isn't the expected behavior in AutoHotkey. Now each event saves the DetectHiddenWindows state when the event is set up and uses that state for window checks.
Create and Show use DetectHiddenWindows On, because Create detects windows before they are shown which means the windows are hidden. This means that the user would need to always set DetectHiddenWindows On before setting up the event, otherwise no windows would be detected. To leave out that pointless step I decided to make the exception for those two. Show usually is usually called once the window is visible, but in some cases (eg Calculator window) it it triggered slightly before becoming fully visible which means AHK can't detect it without DetectHiddenWindows. It seemed reasonable to use DetectHiddenWindows On for that one as well because of that reason.

Archimede
Posts: 550
Joined: 25 Nov 2021, 09:49
Location: Switzerland / Italy

Re: [alpha] WinEvent - easily detect window open, close, move, and more

Post by Archimede » 16 May 2024, 09:41

@Descolada
It seem you have reason about SetTitleMatchMode(...)
Before now I tested a similar thing:

Code: Select all

SetTitleMatchMode( 1 )
WinEvent.Show( FunctionTest, "Senza nome" ) ; <- Italian version of Notepad window begin title
;SetTitleMatchMode( 3 )
Persistent()

FunctionTest( eventObj, hWnd, dwmsEventTime )
{
    sTest := WinGetClass( "ahk_id " . hWnd )
    sTest1 := dwmsEventTime
    sTest2 := WinGetProcessName( "ahk_id " . hWnd )
    sTest3 := WinGetTitle( "ahk_id " . hWnd )

}
and start Notepad: it is detected and call FunctionTest(...).
If I delete ; before SetTitleMatchMode( 3 ) and run the same test, it no detect the Notepad window open.

Now it seem work... WELL!

Please can you explain me the meaning of (*) you used in your example? I don't know it and I no found it in the help manual.

About all other your explain now I try to understand well it.
Thank you very much.

[Mod edit: Added [code][/code] tags. Please use them yourself when posting code!]


Archimede
Posts: 550
Joined: 25 Nov 2021, 09:49
Location: Switzerland / Italy

Re: [alpha] WinEvent - easily detect window open, close, move, and more

Post by Archimede » 16 May 2024, 10:57

@Descolada
I saw it but it no explain the use of (*) alone; it explain the use of * at the end of the last parameter.

Marium0505
Posts: 42
Joined: 11 May 2020, 20:45

Re: [alpha] WinEvent - easily detect window open, close, move, and more

Post by Marium0505 » 16 May 2024, 16:20

@Archimede
It's here:
image.png
image.png (84.11 KiB) Viewed 795 times

Archimede
Posts: 550
Joined: 25 Nov 2021, 09:49
Location: Switzerland / Italy

Re: [alpha] WinEvent - easily detect window open, close, move, and more

Post by Archimede » 16 May 2024, 17:39

@Descolada
Sorry and thank you very much.

Archimede
Posts: 550
Joined: 25 Nov 2021, 09:49
Location: Switzerland / Italy

Re: [alpha] WinEvent - easily detect window open, close, move, and more

Post by Archimede » 22 May 2024, 10:13

@Descolada
Hallo.
I need to close the WinHandle generated by
WinEvent.EventType(...)
I tried to use
EventHook.Stop()
but it seem no destroy it, it seem pauses it.
( if it pauses the handle, what is the difference between
EventHook.Pause(1)
and
EventHook.Stop()
?
If, after used
EventHook.Stop()
I use the EventHook with another function like
EventHook.Pause( -1 )
it seem works well: is it right?
If I need to close the WinEvent handle and free all memory and variable used for that, how I can mak it?
Thank you very much

Descolada
Posts: 1187
Joined: 23 Dec 2021, 02:30

Re: [alpha] WinEvent - easily detect window open, close, move, and more

Post by Descolada » 22 May 2024, 10:45

@Archimede

Code: Select all

#Requires AutoHotkey v2
#include <WinEvent>

npHook := WinEvent.Show(NotepadCreated, "ahk_class Notepad ahk_exe notepad.exe")
Persistent()

NotepadCreated(hook, hWnd, dwmsEventTime) {
    ToolTip "Notepad was created at " dwmsEventTime ", hWnd " hWnd "`n"
    SetTimer ToolTip, -3000
    npHook.Stop()
}

F1::Run("notepad.exe")
F2::npHook.Pause(-1)
F1 causes the event to be triggered. Then pressing F1 again does not trigger the event. Pressing F2 and then F1 still doesn't trigger the event: Stop() worked as expected. Can you demonstrate the code where after using Stop() EventHook.Pause(-1) makes it work again?

Once you use EventHook.Stop() or WinEvent.Stop(EventType, WinTitle, ...) and remove all references to the hook object (eg in my example also used npHook := unset) then all the associated resources are automatically freed.

Archimede
Posts: 550
Joined: 25 Nov 2021, 09:49
Location: Switzerland / Italy

Re: [alpha] WinEvent - easily detect window open, close, move, and more

Post by Archimede » 22 May 2024, 11:05

@Descolada
Ok, I will post the example you asked.
Please, can you explain what is the difference between
EventHook.Pause( 1 )
and
EventHook.Stop()
?

Archimede
Posts: 550
Joined: 25 Nov 2021, 09:49
Location: Switzerland / Italy

Re: [alpha] WinEvent - easily detect window open, close, move, and more

Post by Archimede » 22 May 2024, 11:23

This is the example:

Code: Select all

SetTitleMatchMode( "RegEx" )
DetectHiddenText( False )
hEventHook := WinEvent.Show( FunctionTest, ".+" )
sTest1 := hEventHook.Stop()
sTest2 := hEventHook.Pause( -1 )
sTest3 := ""

FunctionTest( eventObj, hWnd, dwmsEventTime )
{
}
If you execute the example to the line before the last and you see the var values, you can se the variable value
sTest2
is 1

Descolada
Posts: 1187
Joined: 23 Dec 2021, 02:30

Re: [alpha] WinEvent - easily detect window open, close, move, and more

Post by Descolada » 22 May 2024, 12:28

@Archimede quoting from langeek.co:
'Stop' and 'pause' are both terms that involve interruption of an action or process. However, 'stop' indicates a complete and definite termination of an action or process, while 'pause' implies a temporary interruption with an intention to resume.
Meaning after you use EventHook.Stop() the hook is permanently removed and can't be restarted again. Calling hEventHook.Pause( -1 ) "works" in the sense that the internal IsPaused property is changed by it, but the hook is not working and will not work. If you wish I can add a check to EventHook.Pause() to throw an error if the hook is invalid, but I can't imagine a situation where you'd need that.

Archimede
Posts: 550
Joined: 25 Nov 2021, 09:49
Location: Switzerland / Italy

Re: [alpha] WinEvent - easily detect window open, close, move, and more

Post by Archimede » 23 May 2024, 05:07

@Descolada
Hallo.
Like you suggest I made this call:
HookWinClose( &hWinEventHook ) => ( hWinEventHook.Stop(), hWinEventHook := Unset )
It works well and removes the hWinEventHook handle; but I think the better thing is if you make a small modify to allow the call
hWinEventHook.Stop()
to completely delete the variable handle content.

Archimede
Posts: 550
Joined: 25 Nov 2021, 09:49
Location: Switzerland / Italy

Re: [alpha] WinEvent - easily detect window open, close, move, and more

Post by Archimede » 24 May 2024, 07:39

@Descolada
Hallo.
How I can detect if an object is an Event Hook generated by HookWinSet( ... ) or not?
Thank you very much.

Archimede
Posts: 550
Joined: 25 Nov 2021, 09:49
Location: Switzerland / Italy

Re: [alpha] WinEvent - easily detect window open, close, move, and more

Post by Archimede » 27 May 2024, 10:37

Hallo.
I post here the library I modified, starting the core builded by Descolada.
The core (builded by Descolada) is no absolutely modified.
I added all simplified calls and many explanation.
Attachments
WinEvents.ahk
(262.38 KiB) Downloaded 7 times

Descolada
Posts: 1187
Joined: 23 Dec 2021, 02:30

Re: [alpha] WinEvent - easily detect window open, close, move, and more

Post by Descolada » 27 May 2024, 11:20

Archimede wrote:
24 May 2024, 07:39
@Descolada
Hallo.
How I can detect if an object is an Event Hook generated by HookWinSet( ... ) or not?
Thank you very much.
You can use obj is WinEvent to check for that, and obj.EventType to get the name of the event (Show, Close, etc).

Post Reply

Return to “Scripts and Functions (v2)”