[Class] WinHook

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Strange behaviour with Calculator

Post by FanaticGuru » 25 Apr 2022, 15:06

Groot wrote:
25 Apr 2022, 12:21
It works perfectly with Notepad, but give me some very strange results wih Calculator.

Yea, WinHook only makes it simpler to us RegisterShellHookWindow and SetWinEventHook DLLs. It can still be tricky to find the right events to monitor for.

Also, the operating system only knows what the windows tell it and not all windows play nice and follow protocol. And some of the worse windows are the ones built into Windows. I assume to get some special cooler functionality.

For me in Windows 11 the calculator opening and closing works fine if I use "CalculatorApp.exe" and watch for 1 and 2. But there have definitely been cases where I could only figure out what code to use by monitoring them all and picking one that always seemed to occur. There are cases with hi/low bits that are combined in a code which is where you ended up with stuff like 32,772. I see code 32,772 pretty often when a window is activated, on the other hand, I have no idea what code 53 is for.

Also, it could be something with how you are opening the calculator. From the event log, it looks like it is opening, closing, and opening again when you open it. I would just open it manually without a hotkey to test it.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks

Groot
Posts: 22
Joined: 28 Feb 2019, 10:51

Re: [Class] WinHook

Post by Groot » 25 Apr 2022, 15:12

Also, it could be something with how you are opening the calculator. From the event log, it looks like it is opening, closing, and opening again when you open it. I would just open it manually without a hotkey to test it.
I will try that.

Thank you for your reply. :)

ahk7
Posts: 575
Joined: 06 Nov 2013, 16:35

Re: [Class] WinHook

Post by ahk7 » 13 Nov 2022, 04:44

I'm not sure if I want to use this but
1) how do do you know if a WinHook is already created (apart from keeping track of it yourself)
2) does it matter if you create the same hooks again and again; and is there a practical limit

User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: [Class] WinHook

Post by FanaticGuru » 14 Nov 2022, 14:37

ahk7 wrote:
13 Nov 2022, 04:44
I'm not sure if I want to use this but
1) how do do you know if a WinHook is already created (apart from keeping track of it yourself)
2) does it matter if you create the same hooks again and again; and is there a practical limit

There is a Report method that will cause the class to report the hooks that have been created.

If you are creating the same hooks over and over again then you are probably doing something wrong. But you should be able to Add the same hook over and over but I am not sure what you are expecting it to do. Say you Add a shell hook that calls a function when a Notepad window is created. Then you Add that same hook two more times. Now when a Notepad window is created that function will probably be called three times. That might be the desired effect but there is probably a more efficient way to execute the same code three times when a Notepad window is created.

This class is designed to make using shell hooks and event hooks easier to access but you still need to have some understanding of hooks, how they work, and the codes they return. Although fairly simple things can be done with very little knowledge just by following other peoples examples.

For example:

Code: Select all

WinHook.Shell.Add("Created",,, "NOTEPAD.EXE",1) ; Notepad Window Created (1 is the code for created)
WinHook.Shell.Add("Destroyed",,, "NOTEPAD.EXE",2) ; Notepad Window Destroyed (2 is the code for destroyed
It is pretty easy to understand that these two lines cause the function "Created" to be run when a Notepad window is created and the "Destroyed" function when a Notepad window is destroyed.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks

ahk7
Posts: 575
Joined: 06 Nov 2013, 16:35

Re: [Class] WinHook

Post by ahk7 » 14 Nov 2022, 16:59

Thank you for your reply, I hadn't noticed the report method. Reason I'm looking into it is that in my F4MiniMenu script (a script to open one or multiple files for editing in one or more programs simultaneously) is that I'm currently using WinWait for the program to start - but some programs - such as notepad for example - simply starts another one, even if one exists already. So the WinWait is "too soon" (it already exist) or there is another reason it either doesn't wait, or simply waits forever (in case something went wrong).

That is why I'm looking into WinHook as it does see a new notepad window no matter how many I start for example and it doesn't have to WinWait. So if I keep track of the WInHooks created I wouldn't have to create a new one for notepad each time, just the one. I could even go as far as simply create a WinHook for all programs that can be started by F4MiniMenu so I don't have to worry it all I think.

User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: [Class] WinHook

Post by FanaticGuru » 14 Nov 2022, 19:57

ahk7 wrote:
14 Nov 2022, 16:59
I could even go as far as simply create a WinHook for all programs that can be started by F4MiniMenu so I don't have to worry it all I think.

You could do that or just create a shell hook for when any window is created and then in your function check to see if it is one that you want:

Code: Select all

WinHook.Shell.Add("WindowCreated",,,,1) ; call WindowCreated function when any window is created

WindowCreated(Win_Hwnd, Win_Title, Win_Class, Win_Exe, Win_Event)
{
 	ToolTip, % Win_Title "`n" Win_Class "`n" Win_Exe
}
This will display a tooltip showing Title, Class, and Exe of any window created.

You could use Switch or If to do different things based on the information that is passed to the function. Win_Hwnd, Win_Title, Win_Class, Win_Exe, Win_Event

If it is not one of the windows you want to do something with then just return and do nothing in those cases.

You could also create multiple hooks and have them all call the same function or different functions.

The first parameter in the Add method that defines the function to call with shell hook is the only parameter required. All the others wTitle, wClass, wExe, Event are just filters to cut down on the number of events your function will need to deal with. You could leave them all blank and then all the window shell hook events will call your function and your function will then need to deal with them.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks

ahk7
Posts: 575
Joined: 06 Nov 2013, 16:35

Re: [Class] WinHook

Post by ahk7 » 15 Nov 2022, 12:41

Useful tips, thanks. I'll play around with it and see how it works out.

carno
Posts: 265
Joined: 20 Jun 2014, 16:48

Re: [Class] WinHook

Post by carno » 24 Nov 2022, 23:22

Worked great on Win 7 on the first try with no error messages!

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

Re: [Class] WinHook

Post by Archimede » 25 Nov 2022, 04:31

Hallo...
Very interesting.
Please, can you tell me how I can use it to detect:
- the modification of *any* window
- the generation or closing of *any* window.
and no detect when use any window without resize or zooming it.

Thank you very much

User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: [Class] WinHook

Post by FanaticGuru » 28 Nov 2022, 17:44

Here are a couple of tools to monitor all shell and event hooks to try to find the information needed to make a more targeted hook. The 'Event' code sent back is usually the crucial information needed.

WinHook.Shell Monitor

Code: Select all

Gui, +AlwaysOnTop
Gui, Add, Text,, WinHook.Shell
Gui, Add, ListView, r25 w1000, Win_Hwnd|Win_Title|Win_Class|Win_Exe|Win_Event
Gui, Show

WinHook.Shell.Add("AllShell") ; no additional filter parameters result in all windows

AllShell(Win_Hwnd, Win_Title, Win_Class, Win_Exe, Win_Event)
{
	LV_Insert(1, "", Win_Hwnd, Win_Title, Win_Class, Win_Exe, Win_Event)
	Loop, 5
		LV_ModifyCol(A_Index, "AutoHdr")
}

return
GuiClose:
ExitApp
WinHook.Event Monitor

Code: Select all

Gui, +AlwaysOnTop
Gui, Add, Text,, WinHook.Event
Gui, Add, ListView, r25 w1000, hWinEventHook|Event|hWnd|idObject|idChild|dwEventThread|dwmsEventTime|wTitle|wClass
Gui, Show

WinHook.Event.Add(0x0000, 0xFFFF, "AllEvent") ; 0x0000 to 0xFFFF is all events, also with no process specified it defaults to all processes.

AllEvent(hWinEventHook, event, hwnd, idObject, idChild, dwEventThread, dwmsEventTime)
{
	WinGetTitle, wTitle, ahk_id %Hwnd%
	WinGetClass, wClass, ahk_id %Hwnd%
	LV_Insert(1, "", hWinEventHook, event, hwnd, idObject, idChild, dwEventThread, dwmsEventTime, wTitle, wClass)
	Loop, 9
		LV_ModifyCol(A_Index, "AutoHdr")
}

return
GuiClose:
ExitApp

These two scripts were added to the first post.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks

Skrell
Posts: 302
Joined: 23 Jan 2014, 12:05

Re: [Class] WinHook

Post by Skrell » 29 Nov 2022, 10:20

Thank you for the new examples!

david8u8
Posts: 6
Joined: 03 Nov 2022, 10:43

Re: [Class] WinHook

Post by david8u8 » 14 Dec 2022, 04:23

Can you provide v2.0-rc.? I work in v2.0-rc.1. Thank you. great job.

User avatar
andymbody
Posts: 868
Joined: 02 Jul 2017, 23:47

Re: [Class] WinHook

Post by andymbody » 01 Mar 2024, 14:29

Thank you!

chinagreenelvis
Posts: 133
Joined: 19 Oct 2015, 16:21

Re: [Class] WinHook

Post by chinagreenelvis » 18 Mar 2024, 21:27

Has anyone gotten this working in AHK 2?

Post Reply

Return to “Scripts and Functions (v1)”