Trouble getting shellhook to detect window change Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
colt
Posts: 291
Joined: 04 Aug 2014, 23:12
Location: Portland Oregon

Trouble getting shellhook to detect window change

Post by colt » 28 Jun 2018, 11:07

I am attempting to have a function execute when the active window is changed. I looked at a lot of posts about the topic and found this old code snippet. It looks like it will do exactly what I want, but wParam never equals 4 when I activate different windows.
Does anyone know why this is? Is there a better way to detect active window change without using a timer?

Code: Select all

Gui +LastFound 
hWnd := WinExist()
DllCall( "RegisterShellHookWindow", UInt,Hwnd )
MsgNum := DllCall( "RegisterWindowMessage", Str,"SHELLHOOK" )
OnMessage( MsgNum, "ShellMessage" )
Return

ShellMessage( wParam,lParam )
{
 WinGetTitle, title, ahk_id %lParam%
 If (wParam=4) { ;HSHELL_WINDOWACTIVATED
  ToolTip WinActivated`n%Title%
  sleep 1000
  ToolTip
 }
}
I got this snippet from here https://autohotkey.com/board/topic/6672 ... ow-change/

colt
Posts: 291
Joined: 04 Aug 2014, 23:12
Location: Portland Oregon

Re: Trouble getting shellhook to detect window change

Post by colt » 28 Jun 2018, 11:28

I tried editing it to be

Code: Select all

 If ((wParam=4) ||(wParam=32772 )){ ;HSHELL_WINDOWACTIVATED
  ToolTip WinActivated`n%Title%
  sleep 1000
  ToolTip
 }
This partially works, but some windows only trigger it on closing instead of activation/creation. These seem to be windows with ahk_class = #32770.

User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Trouble getting shellhook to detect window change  Topic is solved

Post by jeeswg » 28 Jun 2018, 11:40

You could try this:

Code: Select all

;based on:
;Simple File Explorer Fix - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=5&t=48911

#Persistent
;plays a sound every time the active window is changed
OnWinActiveChange(hWinEventHook, vEvent, hWnd)
{
	;EVENT_SYSTEM_FOREGROUND := 0x3
	static _ := DllCall("user32\SetWinEventHook", UInt,0x3, UInt,0x3, Ptr,0, Ptr,RegisterCallback("OnWinActiveChange"), UInt,0, UInt,0, UInt,0, Ptr)
	DetectHiddenWindows, On
	WinGetClass, vWinClass, % "ahk_id " hWnd
	ToolTip, % vWinClass
	;SoundBeep
	SoundPlay, *64
	;SoundPlay, *48
}
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

colt
Posts: 291
Joined: 04 Aug 2014, 23:12
Location: Portland Oregon

Re: Trouble getting shellhook to detect window change

Post by colt » 28 Jun 2018, 11:46

Brilliant. Thank you!

gongnl
Posts: 91
Joined: 05 Jan 2015, 03:57
Location: /gongnltmp/

Re: Trouble getting shellhook to detect window change

Post by gongnl » 19 Sep 2018, 19:57

Nice thank you

pyn
Posts: 2
Joined: 06 Feb 2020, 11:27

Re: Trouble getting shellhook to detect window change

Post by pyn » 04 Dec 2020, 21:20

This works for me, though I can't see how.

The OnWinActiveChange function is defined, but I don't see it called. Is it called within the function in the RegisterCallback?

I've read the description of hooks at https://docs.microsoft.com/en-us/windows/win32/winmsg/about-hooks and the description of RegisterCallback at https://www.autohotkey.com/docs/commands/RegisterCallback.htm.

I do not understand the creation of the static variable named "_" - is this like "foo" or is it a special variable? https://www.autohotkey.com/docs/Concepts.htm#names indicates it is a valid name.

I don't see the variable named _ used anywhere.

Thanks for any guidance.

User avatar
mikeyww
Posts: 26595
Joined: 09 Sep 2014, 18:38

Re: Trouble getting shellhook to detect window change

Post by mikeyww » 04 Dec 2020, 22:07

Code: Select all

Loop {
 WinWaitNotActive, A
 MsgBox, 64, Results, Windows`, they are a changing.
}

meyer_jing
Posts: 1
Joined: 02 Feb 2023, 21:49

Re: Trouble getting shellhook to detect window change

Post by meyer_jing » 02 Feb 2023, 21:55

jeeswg wrote:
28 Jun 2018, 11:40
You could try this:

Code: Select all

;based on:
;Simple File Explorer Fix - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=5&t=48911

#Persistent
;plays a sound every time the active window is changed
OnWinActiveChange(hWinEventHook, vEvent, hWnd)
{
	;EVENT_SYSTEM_FOREGROUND := 0x3
	static _ := DllCall("user32\SetWinEventHook", UInt,0x3, UInt,0x3, Ptr,0, Ptr,RegisterCallback("OnWinActiveChange"), UInt,0, UInt,0, UInt,0, Ptr)
	DetectHiddenWindows, On
	WinGetClass, vWinClass, % "ahk_id " hWnd
	ToolTip, % vWinClass
	;SoundBeep
	SoundPlay, *64
	;SoundPlay, *48
}
Seems it does not work at win11 (ahk v2)
I've modified it to v2

Code: Select all

OnWinActiveChange(hWinEventHook, vEvent, hWnd)
{
	;EVENT_SYSTEM_FOREGROUND := 0x3
    static _:=DllCall("SetWinEventHook", "UInt",0x3, "UInt",0x3, "Ptr",0, "Ptr",CallbackCreate("OnWinActiveChange"), "UInt",0, "UInt",0, "UInt",0, "Ptr")
    DetectHiddenWindows True
	vWinClass:=WinGetClass("ahk_id " hWnd)
	ToolTip vWinClass
	SoundPlay "*64"
}

Post Reply

Return to “Ask for Help (v1)”