Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

[tool] WinEventHook Messages


  • Please log in to reply
23 replies to this topic
Serenity
  • Members
  • 1271 posts
  • Last active:
  • Joined: 07 Nov 2004
Posted Image
WinEventHook Messages displays messages received via SetWinEventHook. Majkinetor showed us how to use RegisterCallback to receive these messages. I wrote this to catch windows that couldn't be caught by ShellHook method. As with ShellHook Messages there is a message filter. I'm really glad I wrote it now, the message stream updates pretty quickly. ;)

Hooking a particular process/Hooking all processes
WinEventHook Messages hooks all processes by default. You can hook a particular process only by right-clicking in the list and checking "Receive events for this process only" in the popup menu. To restore the hook to all processes, right-click and uncheck.

Message Filter
When checked, these messages will not show.

Hotkeys
Ctrl+C to copy selected line to clipboard
C to clear list
P to pause
R to reload
X to exit

Version History
2008/09/13 v0.3 released
[*:1gzjk509]Replaced DecToHex function with commands to hopefully speed things up a little.
[*:1gzjk509]Some events are received in decimal format, these are now converted to hexadecimal.
[*:1gzjk509]Added option to exclude messages generated by script process. Default is exclude.
[*:1gzjk509]Added hotkey to copy selected line to clipboard.
[*:1gzjk509]Added option to hook a process only. Defaults to hook all processes. 2008/09/12 v0.2 released
[*:1gzjk509]Fixed bug causing some messages to not display.
[*:1gzjk509]Added EVENT_SYSTEM_SOUND message.2008/09/11 v0.1 released

Further reading
Event Constants

; WinEventHook Messages v0.3 by Serenity 
; http://www.autohotkey.com/forum/viewtopic.php?t=35659
#SingleInstance Force
#Persistent
SetBatchLines,-1
; Process, Priority,, High
ExcludeScriptMessages = 1 ; 0 to include
Title := "WinEventHook Messages", Filters := "", Pause := 0
FilterMenu(), Gui()
ahk := WinExist(), WM_VSCROLL := 0x115, SB_BOTTOM := 7
HookProcAdr := RegisterCallback( "HookProc", "F" )
dwFlags := ( ExcludeScriptMessages = 1 ? 0x1 : 0x0 )
hWinEventHook := SetWinEventHook( 0x1, 0x17, 0, HookProcAdr, 0, 0, 0 )
Return

FilterMenu()
{
	Global FilterList
	Menu, Filter, Add, Filter &All, FilterAll
	Menu, Filter, Add, Filter &None, FilterNone
	Menu, Filter, Add
	FilterList = SOUND,ALERT,FOREGROUND,MENUSTART,MENUEND,MENUPOPUPSTART,MENUPOPUPEND
	,CAPTURESTART,CAPTUREEND,MOVESIZESTART,MOVESIZEEND,CONTEXTHELPSTART
	,CONTEXTHELPEND,DRAGDROPSTART,DRAGDROPEND,DIALOGSTART,DIALOGEND,SCROLLINGSTART
	,SCROLLINGEND,SWITCHSTART,SWITCHEND,MINIMIZESTART,MINIMIZEEND
	
	Loop, Parse, FilterList, `,
	{
		If A_Loopfield	
			Menu, Filter, Add, %A_Loopfield%, SetFilter
	}
	Menu, FilterMenu, Add, Message &Filter, :Filter
	Gui, Menu, FilterMenu
}

Gui()
{
	Global
	Gui, +LastFound +AlwaysOnTop +Resize ; +ToolWindow
	Gui, Margin, 0, 0
	Gui, Font, s8, Microsoft Sans Serif
	Gui, Color,, DEDEDE
	Gui, Add, ListView, w600 r10 vData +Grid +NoSort, Hwnd|idObject|idChild|Title|Class|Event|Message
	LV_ModifyCol( 1, 60 ), LV_ModifyCol( 2, 40), LV_ModifyCol( 3, 40)
	LV_ModifyCol( 4, 100 ), LV_ModifyCol( 5, 100 ), LV_ModifyCol( 7, 190 )
	Gui, Show,, %Title%
}

HookProc( hWinEventHook, Event, hWnd, idObject, idChild, dwEventThread, dwmsEventTime )
{
	Global Pause,WM_VSCROLL,SB_BOTTOM,ahk,Filters
	
	SetFormat, Integer, Dec
	Event += 0

	if Event = 1
		Message = EVENT_SYSTEM_SOUND
	else if Event = 2 
		Message = EVENT_SYSTEM_ALERT
	else if Event = 3
		Message = EVENT_SYSTEM_FOREGROUND
	else if Event = 4
		Message = EVENT_SYSTEM_MENUSTART		
	else if Event = 5
		Message = EVENT_SYSTEM_MENUEND
	else if Event = 6
		Message = EVENT_SYSTEM_MENUPOPUPSTART
	else if Event = 7
		Message = EVENT_SYSTEM_MENUPOPUPEND
	else if Event = 8
		Message = EVENT_SYSTEM_CAPTURESTART
	else if Event = 9
		Message = EVENT_SYSTEM_CAPTUREEND
	else if Event = 10
		Message = EVENT_SYSTEM_MOVESIZESTART
	else if Event = 11
		Message = EVENT_SYSTEM_MOVESIZEEND 
	else if Event = 12
		Message = EVENT_SYSTEM_CONTEXTHELPSTART
	else if Event = 13
		Message = EVENT_SYSTEM_CONTEXTHELPEND
	else if Event = 14
		Message = EVENT_SYSTEM_DRAGDROPSTART
	else if Event = 15
		Message = EVENT_SYSTEM_DRAGDROPEND
	else if Event = 16
		Message = EVENT_SYSTEM_DIALOGSTART
	else if Event = 17
		Message = EVENT_SYSTEM_DIALOGEND
	else if Event = 18
		Message = EVENT_SYSTEM_SCROLLINGSTART
	else if Event = 19
		Message = EVENT_SYSTEM_SCROLLINGEND
	else if Event = 20
		Message = EVENT_SYSTEM_SWITCHSTART
	else if Event = 21	
		Message = EVENT_SYSTEM_SWITCHEND
	else if Event = 22
		Message = EVENT_SYSTEM_MINIMIZESTART
	else if Event = 23
		Message = EVENT_SYSTEM_MINIMIZEEND
	
	Sleep, 50 ; give a little time for WinGetTitle/WinGetActiveTitle functions, otherwise they return blank
	
	EventHex := Event
	SetFormat, Integer, Hex
	EventHex += 0, hWnd += 0, idObject += 0, idChild += 0 

	If Event not in %Filters%
	{
		If ( Pause = 0 )
		{
			LV_Add( "", hWnd, idObject, idChild, WinGetTitle(hWnd), WinGetClass(hWnd), EventHex, Message )		
		}
		SendMessage, WM_VSCROLL, SB_BOTTOM, 0, SysListView321, ahk_id %ahk%
	}	
}

SetFilter:
Menu, Filter, ToggleCheck, %A_ThisMenuItem%
Loop, Parse, FilterList, `,
{
	If ( A_ThisMenuItem = A_Loopfield )
	{
		If A_Index in %Filters% ; remove from filter
		{		
			Filter := A_Index
			Loop, Parse, Filters, `,
			{
				If ( A_Loopfield != Filter )
					NewFilters .= A_Loopfield . ( A_Loopfield != "" ? "`," : "" ) 
			}
			Filters := NewFilters, NewFilters := ""
		}	
		Else ; add to filter
		{
			Filters .= A_Index . ","
		}	
	}	
}
Return

FilterAll:
Filters =
Loop, Parse, FilterList, `,
{	
	Menu, Filter, Check, %A_Loopfield%
	Filters .= A_Index . ( A_Index != 23 ? "," : "" ) 
}
Return

FilterNone:
Loop, Parse, FilterList, `,
{
	Menu, Filter, UnCheck, %A_Loopfield%
	Filters = 
}
Return

WinGetTitle( hwnd )
{
	WinGetTitle, wtitle, ahk_id %hwnd%
	Return wtitle
}

WinGetClass( hwnd )
{
	WinGetClass, wclass, ahk_id %hwnd%
	Return wclass
}

SetWinEventHook(eventMin, eventMax, hmodWinEventProc, lpfnWinEventProc, idProcess, idThread, dwFlags) 
{
	DllCall("CoInitialize", Uint, 0)
	return DllCall("SetWinEventHook"
	, Uint,eventMin	
	, Uint,eventMax	
	, Uint,hmodWinEventProc
	, Uint,lpfnWinEventProc
	, Uint,idProcess
	, Uint,idThread
	, Uint,dwFlags)	
}

NewHook()
{
	Global
	
	If HookSelected = 1
	{
		LV_GetText( SelHwnd, LV_GetNext(0, "Focused") )
		WinGet, idProcess, PID, ahk_id %SelHwnd% 
		If idProcess =
		{
			Menu, Ctx, ToggleCheck, &Receive events from this process only
			HookSelected := !HookSelected
			Return
		}
	}
	Else
	{
		idProcess = 0 ; hook all
	}
	UnhookWinEvent() 
	HookProcAdr := RegisterCallback( "HookProc", "F" ) ; new hook 
	hWinEventHook := SetWinEventHook( 0x1, 0x17, 0, HookProcAdr, idProcess, 0, dwFlags )	
}

UnhookWinEvent()
{
	Global
	DllCall( "UnhookWinEvent", Uint,hWinEventHook )
	DllCall( "GlobalFree", UInt,&HookProcAdr ) ; free up allocated memory for RegisterCallback
}

GuiContextMenu:
Menu, Ctx, Add, &Receive events from this process only, HookMode
Menu, Ctx, Show
Return

HookMode:
Menu, Ctx, ToggleCheck, &Receive events from this process only
HookSelected := !HookSelected
NewHook()
Return

GuiSize:
GuiControl, Move, Data, w%A_GuiWidth% h%A_GuiHeight%
SendMessage, WM_VSCROLL, SB_BOTTOM, 0, SysListView321, ahk_id %ahk%
Return

GuiClose:
GuiEscape:
ExitApp
Return

#IfWinActive WinEventHook Messages
	C::LV_Delete()
	
	P::
	Pause :=! Pause, WinTitle := ( Pause = 0 ? Title : Title . " (Paused)" )
	WinSetTitle %WinTitle%
	Return
	
	R::Reload
	X::ExitApp	
	
	^C::
	Clipboard = 
	Loop, % LV_GetCount("Col")
	{
		LV_GetText( lv%A_Index%, LV_GetNext(0, "Focused"), A_Index ) 
		Clipboard .= lv%A_Index% . ( A_Index != LV_GetCount("Col") ? "|" : "" )
	}
	Return
#IfWinActive

; Event Constants
EVENT_SYSTEM_SOUND = 0x1
EVENT_SYSTEM_ALERT = 0x2
EVENT_SYSTEM_FOREGROUND = 0x3
EVENT_SYSTEM_MENUSTART = 0x4 
EVENT_SYSTEM_MENUEND = 0x5
EVENT_SYSTEM_MENUPOPUPSTART = 0x6
EVENT_SYSTEM_MENUPOPUPEND = 0x7
EVENT_SYSTEM_CAPTURESTART = 0x8
EVENT_SYSTEM_CAPTUREEND = 0x9
EVENT_SYSTEM_MOVESIZESTART = 0xa
EVENT_SYSTEM_MOVESIZEEND = 0xb
EVENT_SYSTEM_CONTEXTHELPSTART = 0xc
EVENT_SYSTEM_CONTEXTHELPEND = 0xd
EVENT_SYSTEM_DRAGDROPSTART = 0xe
EVENT_SYSTEM_DRAGDROPEND = 0xf
EVENT_SYSTEM_DIALOGSTART = 0x10
EVENT_SYSTEM_DIALOGEND = 0x11
EVENT_SYSTEM_SCROLLINGSTART = 0x12
EVENT_SYSTEM_SCROLLINGEND = 0x13
EVENT_SYSTEM_SWITCHSTART = 0x14
EVENT_SYSTEM_SWITCHEND = 0x15
EVENT_SYSTEM_MINIMIZESTART = 0x16
EVENT_SYSTEM_MINIMIZEEND = 0x17


tank
  • Moderators
  • 4238 posts
  • Last active: Yesterday, 02:20 PM
  • Joined: 21 Dec 2007
this is also a very very nice tool ill use this and [tool] ShellHook Messages alot

  • Guests
  • Last active:
  • Joined: --
8) Wow thank you very much 8)

:shock: You gotta be a genious :wink:

Serenity
  • Members
  • 1271 posts
  • Last active:
  • Joined: 07 Nov 2004
Aw, thank you both. :) Guest, that's sweet of you to say.

I found some bugs and fixed them in v0.2. See first post for details.

Serenity
  • Members
  • 1271 posts
  • Last active:
  • Joined: 07 Nov 2004
v0.3 released:
[*:23rs0qhl]Replaced DecToHex function with commands to hopefully speed things up a little.
[*:23rs0qhl]Some events are received in decimal format, these are now converted to hexadecimal.
[*:23rs0qhl]Added option to exclude messages generated by script process. Default is exclude.
[*:23rs0qhl]Added hotkey to copy selected line to clipboard.
[*:23rs0qhl]Added option to hook a process only. Defaults to hook all processes.

Serenity
  • Members
  • 1271 posts
  • Last active:
  • Joined: 07 Nov 2004
Here's an example of a popup blocker using Notepad. The first two parameters in the call to SetWinEventHook() are set to 0x3 to only receive EVENT_SYSTEM_FOREGROUND messages.

#SingleInstance Force
#Persistent
SetBatchLines,-1
HookProcAdr := RegisterCallback( "HookProc", "F" )
hWinEventHook := SetWinEventHook( 0x3, 0x3, 0, HookProcAdr, 0, 0, 0 )
OnExit, HandleExit
Return

HookProc( hWinEventHook, Event, hWnd, idObject, idChild, dwEventThread, dwmsEventTime )
{
	if Event ; EVENT_SYSTEM_FOREGROUND = 0x3
	{	
		WinGetClass, class, ahk_id %hWnd%
		If class = Notepad
			WinClose, ahk_class Notepad		
	}
}

SetWinEventHook(eventMin, eventMax, hmodWinEventProc, lpfnWinEventProc, idProcess, idThread, dwFlags)
{
	DllCall("CoInitialize", Uint, 0)
	return DllCall("SetWinEventHook"
	, Uint,eventMin	
	, Uint,eventMax	
	, Uint,hmodWinEventProc
	, Uint,lpfnWinEventProc
	, Uint,idProcess
	, Uint,idThread
	, Uint,dwFlags)	
}

UnhookWinEvent()
{
	Global
	DllCall( "UnhookWinEvent", Uint,hWinEventHook )
	DllCall( "GlobalFree", UInt,&HookProcAdr ) ; free up allocated memory for RegisterCallback
}

HandleExit:
UnhookWinEvent()
ExitApp
Return


DeWild1
  • Members
  • 369 posts
  • Last active: Feb 28 2014 08:15 PM
  • Joined: 30 Apr 2006
Thank you for your PM with the same example.. (I post here as well for it may help others) :D

I have spent much time playing with it and I found it to be very efficient and light on the CPU.

Questions:

1. I can not get it to work at all for FireFox or IE. For normal windows, it works much better than SHELLHOOK but SHELLHOOK did work every now and then for IE and FF...
:cry: :cry: I may have to settle for image search :cry: :cry: or something like this,,,
#Persistent 
settitlematchmode, 1 ;starts with......
WinTitle = bla bla bla ;some IE window
SetTimer, Checkwindow, 50 
return 

Checkwindow: 
  If WinActive( WinTitle ) 
    WinClose ;or whatever..
Return

2. I know this mainly works for window messages, and this question will make me seem dumber than I already look, (but I have NO shame :p ), is there anyway to sense a new process being started?

  • Guests
  • Last active:
  • Joined: --

Here's an example of a popup blocker using Notepad. The first two parameters in the call to SetWinEventHook() are set to 0x3 to only receive EVENT_SYSTEM_FOREGROUND messages.

#SingleInstance Force
#Persistent
SetBatchLines,-1
HookProcAdr := RegisterCallback( "HookProc", "F" )
hWinEventHook := SetWinEventHook( 0x3, 0x3, 0, HookProcAdr, 0, 0, 0 )
OnExit, HandleExit
Return

HookProc( hWinEventHook, Event, hWnd, idObject, idChild, dwEventThread, dwmsEventTime )
{
	if Event ; EVENT_SYSTEM_FOREGROUND = 0x3
	{	
		WinGetClass, class, ahk_id %hWnd%
		If class = Notepad
			WinClose, ahk_class Notepad		
	}
}

SetWinEventHook(eventMin, eventMax, hmodWinEventProc, lpfnWinEventProc, idProcess, idThread, dwFlags)
{
	DllCall("CoInitialize", Uint, 0)
	return DllCall("SetWinEventHook"
	, Uint,eventMin	
	, Uint,eventMax	
	, Uint,hmodWinEventProc
	, Uint,lpfnWinEventProc
	, Uint,idProcess
	, Uint,idThread
	, Uint,dwFlags)	
}

UnhookWinEvent()
{
	Global
	DllCall( "UnhookWinEvent", Uint,hWinEventHook )
	DllCall( "GlobalFree", UInt,&HookProcAdr ) ; free up allocated memory for RegisterCallback
}

HandleExit:
UnhookWinEvent()
ExitApp
Return


Serenity, this works swell in XP, but can you tell why this may not work in Vista?, what is wrong :)

Serenity
  • Members
  • 1271 posts
  • Last active:
  • Joined: 07 Nov 2004

I can not get it to work at all for FireFox or IE.


It works for me on 2k with Explorer messages (Confirm File Delete etc.) and Firefox. Try this:

#SingleInstance Force
#Persistent
SetBatchLines,-1
HookProcAdr := RegisterCallback( "HookProc", "F" )
hWinEventHook := SetWinEventHook( 0x3, 0x3, 0, HookProcAdr, 0, 0, 0 )
OnExit, HandleExit
Return

HookProc( hWinEventHook, Event, hWnd, idObject, idChild, dwEventThread, dwmsEventTime )
{
   if Event ; EVENT_SYSTEM_FOREGROUND = 0x3
   {   
      WinGetTitle, title, ahk_id %hWnd%
      If title = The page at http://www.autohotkey.com says: ; code copy popup
         WinClose, ahk_id %hWnd%			
   }
}

SetWinEventHook(eventMin, eventMax, hmodWinEventProc, lpfnWinEventProc, idProcess, idThread, dwFlags)
{
   DllCall("CoInitialize", Uint, 0)
   return DllCall("SetWinEventHook"
   , Uint,eventMin   
   , Uint,eventMax   
   , Uint,hmodWinEventProc
   , Uint,lpfnWinEventProc
   , Uint,idProcess
   , Uint,idThread
   , Uint,dwFlags)   
}

UnhookWinEvent()
{
   Global
   DllCall( "UnhookWinEvent", Uint,hWinEventHook )
   DllCall( "GlobalFree", UInt,&HookProcAdr ) ; free up allocated memory for RegisterCallback
}

HandleExit:
UnhookWinEvent()
ExitApp
Return

is there anyway to sense a new process being started?


Maybe with another type of system hook. I wrote a script a while back to monitor for new processes but it is timer based.

Serenity, this works swell in XP, but can you tell why this may not work in Vista?, what is wrong Smile


I don't know why it won't work in Vista. Could you try the script in the first post to see if EVENT_SYSTEM_FOREGROUND is being sent?

  • Guests
  • Last active:
  • Joined: --

Serenity, this works swell in XP, but can you tell why this may not work in Vista?, what is wrong Smile


I don't know why it won't work in Vista. Could you try the script in the first post to see if EVENT_SYSTEM_FOREGROUND is being sent?


Hello there Serenity. Hope you are going to have a wonderful day!

When I add this line below, I do receive a 0x3 response on Vista :)

HookProc( hWinEventHook, Event, hWnd, idObject, idChild, dwEventThread, dwmsEventTime )
{
   if Event ; EVENT_SYSTEM_FOREGROUND = 0x3
   {   

[color=red]tooltip, %event%[/color]

      WinGetClass, class, ahk_id %hWnd%
      If class = Notepad
         WinClose, ahk_class Notepad      
   }
}


Basically, just to sum it up..., I was wishing for something here I can turn on and off, that will block any & all new windows,guis,dialougs--all of them--from ever appearing, ... ideally before such Windows are created. :) Can this be done? :D Thank you so much.

kindest regards :) :)

DeWild1
  • Members
  • 369 posts
  • Last active: Feb 28 2014 08:15 PM
  • Joined: 30 Apr 2006

Serenity, this works swell in XP, but can you tell why this may not work in Vista?, what is wrong Smile


I don't know why it won't work in Vista. Could you try the script in the first post to see if EVENT_SYSTEM_FOREGROUND is being sent?


Hello there Serenity. Hope you are going to have a wonderful day!

When I add this line below, I do receive a 0x3 response on Vista :)

HookProc( hWinEventHook, Event, hWnd, idObject, idChild, dwEventThread, dwmsEventTime )
{
   if Event ; EVENT_SYSTEM_FOREGROUND = 0x3
   {   

[color=red]tooltip, %event%[/color]

      WinGetClass, class, ahk_id %hWnd%
      If class = Notepad
         WinClose, ahk_class Notepad      
   }
}


Basically, just to sum it up..., I was wishing for something here I can turn on and off, that will block any & all new windows,guis,dialougs--all of them--from ever appearing, ... ideally before such Windows are created. :) Can this be done? :D Thank you so much.

kindest regards :) :)


Ya know, Vista sucks.. I thought I had the same problem with another type of hook... <!-- m -->http://www.autohotke... ... 730#233730<!-- m -->
As it turned out, I rebooted and all was well..
I am having trouble right now with AHK - Vista with it even simply activating a window... Something that has always worked and the same code worked fine 3 or 4 days ago... Then I realized - remembered, (after reading your post), that I have not rebooted in about a week... :roll: Stupid Vista...

Serenity, I will test now and after a reboot.. :twisted: :roll: :cry:

DeWild1
  • Members
  • 369 posts
  • Last active: Feb 28 2014 08:15 PM
  • Joined: 30 Apr 2006
Swwwweeetness.. Did not have to reboot, 8) and it works.. You are my HERO!!!!!!!!

Thank you!!!!!!!!!! :wink: :D

BTW, I changed it to
	    If InStr( Title, "some_title_name_with_no_spaces" ) 
and FYI, you have to have it do a msgbox, then winactivate to be 100% dependable.... Otherwise it did not detect it.. (I am sure a reboot would help my stupid Vista though)

G40
  • Guests
  • Last active:
  • Joined: --
I am curious about this process below, too, ... Serenity, is it possible to create a function which basically totally eliminates any new popups whilst it is activated?

Serenity, this works swell in XP, but can you tell why this may not work in Vista?, what is wrong Smile


I don't know why it won't work in Vista. Could you try the script in the first post to see if EVENT_SYSTEM_FOREGROUND is being sent?


Hello there Serenity. Hope you are going to have a wonderful day!

When I add this line below, I do receive a 0x3 response on Vista :)

HookProc( hWinEventHook, Event, hWnd, idObject, idChild, dwEventThread, dwmsEventTime )
{
   if Event ; EVENT_SYSTEM_FOREGROUND = 0x3
   {   

[color=red]tooltip, %event%[/color]

      WinGetClass, class, ahk_id %hWnd%
      If class = Notepad
         WinClose, ahk_class Notepad      
   }
}


Basically, just to sum it up..., I was wishing for something here I can turn on and off, that will block any & all new windows,guis,dialougs--all of them--from ever appearing, ... ideally before such Windows are created. :) Can this be done? :D Thank you so much.

kindest regards :) :)



Zoplike
  • Guests
  • Last active:
  • Joined: --
Just curious, is this even possible ... for some kinda function which can be set on or off (when needed), for the blockage of all desktop dialogs or new GUI instances,... aka. to quiet things down a bit? :) Like a super-charged Browser pop-up ad blocker, ... but for the entire windows system? :)

pantagruel
  • Members
  • 97 posts
  • Last active: Oct 01 2013 08:18 PM
  • Joined: 08 Oct 2006
Hi,
UPDATED: Never mind, it was embarrassingly obvious once I took a break and then went back and looked at the code.
I want to catch different types of window opening events, for example system alerts, to do this I have the following:
#SingleInstance Force
#Persistent
SetBatchLines,-1
HookProcAdr := RegisterCallback( "HookProc", "F" )
hWinEventHook := SetWinEventHook( 0x3, 0x3, 0, HookProcAdr, 0, 0, 0 )
OnExit, HandleExit
Return

HookProc( hWinEventHook, Event, hWnd, idObject, idChild, dwEventThread, dwmsEventTime )
{
  
if Event ; EVENT_SYSTEM_ALERT = 0x2
   {   

      WinGetTitle, title, ahk_id %hWnd%
FileAppend,EVENT_SYSTEM_ALERT  %A_NOW% %title% %hWnd% `n,c:\temp\autohotkey.log
	If InStr( title, "Password" ){
WinClose,ahk_id %hWnd%

	}
  }
 if Event ; EVENT_SYSTEM_DIALOGSTART = 0x10
   {   

      WinGetTitle, title, ahk_id %hWnd%
FileAppend, EVENT_SYSTEM_DIALOGSTART  %A_NOW% %title% %hWnd% `n,c:\temp\autohotkey.log
	If InStr(title, "Password" ){

WinClose,ahk_id %hWnd%

	}}
     
         
 
}

SetWinEventHook(eventMin, eventMax, hmodWinEventProc, lpfnWinEventProc, idProcess, idThread, dwFlags)
{
   DllCall("CoInitialize", Uint, 0)
   return DllCall("SetWinEventHook"
   , Uint,eventMin   
   , Uint,eventMax   
   , Uint,hmodWinEventProc
   , Uint,lpfnWinEventProc
   , Uint,idProcess
   , Uint,idThread
   , Uint,dwFlags)   
}

UnhookWinEvent()
{
   Global
   DllCall( "UnhookWinEvent", Uint,hWinEventHook )
   DllCall( "GlobalFree", UInt,&HookProcAdr ) ; free up allocated memory for RegisterCallback
}

HandleExit:
UnhookWinEvent()
ExitApp
Return

now one thing you might notice is that where the event is caught I have a fileappend, I would actually like that in the If block where I find out if it matches the title I am looking for but if I do that the alert which I would like to click on is never clicked on.
In fact without this fileappend or some similar thing I have to basically click on another window and then click back on the window that has popped up the alert.

Now as for the info that get's appended to that file:

EVENT_SYSTEM_ALERT 20090619142657 C:\RenderingService 24248482
EVENT_SYSTEM_DIALOGSTART 20090619142657 C:\RenderingService 24248482
EVENT_SYSTEM_ALERT 20090619142659 \\Siasgb31\test files 21168570
EVENT_SYSTEM_DIALOGSTART 20090619142659 \\Siasgb31\test files 21168570
EVENT_SYSTEM_ALERT 20090619142701 Microsoft Word 36897788
EVENT_SYSTEM_DIALOGSTART 20090619142701 Microsoft Word 36897788
EVENT_SYSTEM_ALERT 20090619142702 9110210
EVENT_SYSTEM_DIALOGSTART 20090619142702 Password 9110210
EVENT_SYSTEM_ALERT 20090619142702 Microsoft Word 36897788
EVENT_SYSTEM_DIALOGSTART 20090619142702 Microsoft Word 36897788
EVENT_SYSTEM_ALERT 20090619142709 autohotkey.log - Notepad 17498464
EVENT_SYSTEM_DIALOGSTART 20090619142709 autohotkey.log - Notepad 17498464
EVENT_SYSTEM_ALERT 20090619142709 autohotkey.log - Notepad 17498464
EVENT_SYSTEM_DIALOGSTART 20090619142709 autohotkey.log - Notepad 17498464
EVENT_SYSTEM_ALERT 20090619142711 196702
EVENT_SYSTEM_DIALOGSTART 20090619142711 196702
EVENT_SYSTEM_ALERT 20090619142712 C:\RenderingService 24248482
EVENT_SYSTEM_DIALOGSTART 20090619142712 C:\RenderingService 24248482
EVENT_SYSTEM_ALERT 20090619142714 C:\Temp 4457096
EVENT_SYSTEM_DIALOGSTART 20090619142714 C:\Temp 4457096
EVENT_SYSTEM_ALERT 20090619142716 Untitled - Notepad 7995806
EVENT_SYSTEM_DIALOGSTART 20090619142716 Untitled - Notepad 7995806
EVENT_SYSTEM_ALERT 20090619142812 C:\Temp 4457096
EVENT_SYSTEM_DIALOGSTART 20090619142812 C:\Temp 4457096
EVENT_SYSTEM_ALERT 20090619142814 Microsoft Word 36897788
EVENT_SYSTEM_DIALOGSTART 20090619142814 Microsoft Word 36897788
EVENT_SYSTEM_ALERT 20090619142814 Microsoft Word 36897788
EVENT_SYSTEM_DIALOGSTART 20090619142814 Microsoft Word 36897788
EVENT_SYSTEM_ALERT 20090619142817 196702
EVENT_SYSTEM_DIALOGSTART 20090619142817 196702
EVENT_SYSTEM_ALERT 20090619142818 C:\Temp 4457096
EVENT_SYSTEM_DIALOGSTART 20090619142818 C:\Temp 4457096
EVENT_SYSTEM_ALERT 20090619142819 Untitled - Notepad 19071492
EVENT_SYSTEM_DIALOGSTART 20090619142819 Untitled - Notepad 19071492
EVENT_SYSTEM_ALERT 20090619142828 C:\Temp 4457096
EVENT_SYSTEM_DIALOGSTART 20090619142828 C:\Temp 4457096
EVENT_SYSTEM_ALERT 20090619142829 Untitled - Notepad 19137102
EVENT_SYSTEM_DIALOGSTART 20090619142829 Untitled - Notepad 19137102
EVENT_SYSTEM_ALERT 20090619142835 C:\Temp 4457096
EVENT_SYSTEM_DIALOGSTART 20090619142835 C:\Temp 4457096
EVENT_SYSTEM_ALERT 20090619142837 WinEventHandler.ahk - Notepad 7275152
EVENT_SYSTEM_DIALOGSTART 20090619142837 WinEventHandler.ahk - Notepad 7275152
EVENT_SYSTEM_ALERT 20090619142901 196702
EVENT_SYSTEM_DIALOGSTART 20090619142901 196702
EVENT_SYSTEM_ALERT 20090619142901 18874774
EVENT_SYSTEM_DIALOGSTART 20090619142902 18874774
EVENT_SYSTEM_ALERT 20090619151752 \\Siasgb31\test files 21168570
EVENT_SYSTEM_DIALOGSTART 20090619151752 \\Siasgb31\test files 21168570
EVENT_SYSTEM_ALERT 20090619151754 Microsoft Word 9765434
EVENT_SYSTEM_DIALOGSTART 20090619151754 Microsoft Word 9765434
EVENT_SYSTEM_ALERT 20090619151755 25428762
EVENT_SYSTEM_DIALOGSTART 20090619151755 Password 25428762
EVENT_SYSTEM_ALERT 20090619151755 Microsoft Word 9765434
EVENT_SYSTEM_DIALOGSTART 20090619151755 Microsoft Word 9765434
EVENT_SYSTEM_ALERT 20090619151759 \\Siasgb31\test files 21168570
EVENT_SYSTEM_DIALOGSTART 20090619151759 \\Siasgb31\test files 21168570
EVENT_SYSTEM_ALERT 20090619151801 C:\Temp 4457096
EVENT_SYSTEM_DIALOGSTART 20090619151801 C:\Temp 4457096
EVENT_SYSTEM_ALERT 20090619151802 Untitled - Notepad 8126598
EVENT_SYSTEM_DIALOGSTART 20090619151802 Untitled - Notepad 8126598


Which seems wrong to me, most of these windows are not alerts or dialogs of any sort. Basically what it seems to be doing is the same thing as looping through Windows, which I don't want.

I know this currently would be doable by looping through the windows etc. but the reason I am using this is I would like to achieve a greater deal of control.

So anyway, my questions are:

1. How can I make it so it just catches the event of an alert box?
2. How can I make it so that I don't have to do something like the fileappend to catch the alert and close it?
3. Can anyone explain why that happens - that if the fileappend is there I close the alert, and if it isn't there it just hangs around forever?
If it affects anything this is an alert raised from Word, but I am going to want to be able to catch alerts from all sorts of different applications.

As an easier way to test this, if I have the code:


if Event ; EVENT_SYSTEM_ALERT = 0x2
   {   
	If InStr( title, "test.ahk" ){
WinActivate
Send,{Escape}
	}
  }

and a file named test.ahk with the following:
msgbox,hi


If I start test.ahk my alert ("hi") hangs around forever, but it will be closed if I move the focus to another window, and then back to the alert

But if the code in my event catching autohotkey script is the following:


if Event ; EVENT_SYSTEM_ALERT = 0x2
   {   
FileAppend,EVENT_SYSTEM_ALERT  %A_NOW% %hWnd% `n,c:\temp\autohotkey.log 
      WinGetTitle, title, ahk_id %hWnd%
	If InStr( title, "test.ahk" ){

WinActivate
Send,{Escape}
	}
  }

then nearly instantaneously with the alert popping up it is killed. However when I look in the log there is not just one message about that particular window that I just killed but every other window on the system, despite them not being alerts (as per the earlier quoted example)

WHY???!??

(damn, I hope this was reasonably clear)