Trouble getting shellhook to detect window change Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
colt
Posts: 105
Joined: 04 Aug 2014, 23:12
Location: Portland Oregon

Trouble getting shellhook to detect window change

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: 105
Joined: 04 Aug 2014, 23:12
Location: Portland Oregon

Re: Trouble getting shellhook to detect window change

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: 6655
Joined: 19 Dec 2016, 01:58
Location: UK

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

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: 105
Joined: 04 Aug 2014, 23:12
Location: Portland Oregon

Re: Trouble getting shellhook to detect window change

28 Jun 2018, 11:46

Brilliant. Thank you!
gongnl
Posts: 72
Joined: 05 Jan 2015, 03:57
Location: /gongnltmp/

Re: Trouble getting shellhook to detect window change

19 Sep 2018, 19:57

Nice thank you

Return to “Ask For Help”

Who is online

Users browsing this forum: kkleinfelter, Milenko and 147 guests