jerebenz

Capture the Windows events

7 posts in this topic

Dear All,

First post for me, and I want to thank the Autoit Team for this amazing work you did and you still doing.

I've read the forum for hours searching for a "simple" way to capture the windows events. I've found some topics, but I thought it was too complicated, and I was affraid my program could become unstable.

Please let me explain my need :

I want to create a toolbar for a software I'm using.

I created a GUI, and then attached the GUI to the program using WinAPI (I simplyfied the code) :

#include <WindowsConstants.au3>
#Include <WinAPI.au3>

; Getting the Handle to the window I want to attach my toolbar to
$Active_Program_Window = WinWaitActive("[TITLE:My Program:]")

; Creating my GUI
$My_Toolbar = GUICreate("", 200, 37, 10, 10, $WS_POPUP)

; Setting the program window as parent of the toolbar
_WinAPI_SetParent($My_Toolbar, $Active_Program_Window)

With this code, the GUI is automaticaly moving with the main window. This part is okay.

What I want to do, is to be able to resize the GUI when the main window is resizing. To do that, I need to be able to capture the event when the window is resized, and apply my gui resize user function.

I found an exemple in this thread, but it doesn't capture the windows resize event : http://www.autoitscript.com/forum/index.php?showtopic=56536&st=0

Thanks in advance for your help !

Best Regards,

Jérémie

Share this post


Link to post
Share on other sites



Hey All,

-- 24h Bump --

I found the folowing topic : http://www.autoitscript.com/forum/index.php?showtopic=93315&st=0&p=670522&hl=windows%20event%20hook&fromsearch=1&#entry670522

The code is capturing some events of the "explorer.exe" process.

;Detect foreground windows with SetWinEventHook
;Author; rover 04/17/09
#include <WindowsConstants.au3>
#include <WinAPI.au3>
#include <Process.au3>
#include <Misc.au3>

Opt("MustDeclareVars", 1)

Global $hLasthWnd, $hDLL, $hWinEventProc, $hHook
_Singleton("DialogHookDemo", 0)

HotKeySet("{ESC}", "_HotKey") ; Exit
HotKeySet("{F9}", "_HotKey")  ; Start/Stop dialog box monitoring

Global Const $DWL_DLGPROC = 4
Global Const $DWL_MSGRESULT = 0
Global Const $DWL_USER = 8

;Event Constants
;http://msdn.microsoft.com/en-us/library/ms697187.aspx
;http://msdn.microsoft.com/en-us/library/dd318066(VS.85).aspx
;http://mwinapi.sourceforge.net/doc/html/T_ManagedWinapi_Accessibility_AccessibleEventType.htm
Global Const $EVENT_SYSTEM_FOREGROUND = 0x3
Global Const $EVENT_SYSTEM_MENUSTART = 0x4
Global Const $EVENT_SYSTEM_MENUEND = 0x5
Global Const $EVENT_SYSTEM_MENUPOPUPSTART = 0x6
Global Const $EVENT_SYSTEM_MENUPOPUPEND = 0x7
Global Const $EVENT_SYSTEM_DIALOGEND = 0x11 ; #32770 dialog box class
Global Const $EVENT_SYSTEM_DIALOGSTART = 0x10 ; #32770 dialog box class
Global Const $EVENT_OBJECT_CREATE = 0x8000
Global Const $EVENT_OBJECT_DESTROY = 0x8001
Global Const $EVENT_OBJECT_SHOW = 0x8002

;probably best to use SYSTEM_FOREGROUND as OBJECT_CREATE will trigger far more frequently
;even with additional filtering in _WinEventProc

;minimum and maximum events
;to monitor one event type only use same event for min/max
Global $EVENT_Min = $EVENT_SYSTEM_FOREGROUND ; $EVENT_OBJECT_CREATE
Global $EVENT_Max = $EVENT_SYSTEM_FOREGROUND ; $EVENT_OBJECT_CREATE ; $EVENT_OBJECT_DESTROY

;disables Yes/No buttons on Rename file extension dialog for 5 seconds
;or disable Run dialog
;Global $sWinTitle = "Run"
Global $sWinTitle = "Rename"

ConsoleWrite("! Press F9 to start/stop monitoring, ESC to exit" & @CRLF)

While 1
    Sleep(1000)
WEnd

Func _WinEventProc($hHook, $iEvent, $hWnd, $idObject, $idChild, $iEventThread, $iEventTime)
    ;If $idObject <> 0 Or $iEvent <> $EVENT_Min Then Return ;Global Const $OBJID_WINDOW = 0x0
    If StringCompare(WinGetTitle($hWnd), $sWinTitle) <> 0 Then Return
    Local $sTitle = WinGetTitle($hWnd)
    WinSetTitle($hWnd, "", $sTitle & " - Disabled for 5 Seconds")
    WinSetState($hWnd, "", @SW_DISABLE) ; will disable dialog and titlebar close button
    ControlDisable($hWnd, "", "[CLASS:Button; INSTANCE:1]") ;show change in dialog by greying buttons
    ControlDisable($hWnd, "", "[CLASS:Button; INSTANCE:2]")
    WinFlash($hWnd, "", 5, 500) ;further indication by flashing title and titlebar close button
    ;Sleep(5000)
    ControlEnable($hWnd, "", "[CLASS:Button; INSTANCE:1]")
    ControlEnable($hWnd, "", "[CLASS:Button; INSTANCE:2]")
    WinSetTitle($hWnd, "", $sTitle)
    WinSetState($hWnd, "", @SW_ENABLE)
EndFunc   ;==>_WinEventProc

Func _SetWinEventHook($iEventMin, $iEventMax, $hDLLUser32 = -1)
    ;http://msdn.microsoft.com/en-us/library/ms696160.aspx
    ;Author: rasim (from WinTray.au3)
    Local $aRet
    Local Const $WINEVENT_OUTOFCONTEXT = 0x0
    Local Const $WINEVENT_SKIPOWNPROCESS = 0x2
    If Not $hDLLUser32 Or $hDLLUser32 = -1 Then $hDLLUser32 = "User32.dll"
    $aRet = DllCall($hDLLUser32, "hwnd", "SetWinEventHook", _
            "uint", $iEventMin, "uint", $iEventMax, "hwnd", 0, _
            "ptr", DllCallbackGetPtr($hWinEventProc), "int", 0, _
            "int", 0, "uint", BitOR($WINEVENT_OUTOFCONTEXT, $WINEVENT_SKIPOWNPROCESS))
    If @error Then Return SetError(@error, 0, 0)
    Return $aRet[0]
EndFunc   ;==>_SetWinEventHook

Func _HotKey()
    Switch @HotKeyPressed
        Case "{F9}"
            If Not $hDLL Then ;; Start dialog monitoring
                $hDLL = DllOpen("User32.dll")
                If Not $hDLL Then Return ConsoleWrite("DllOpen('User32.dll') failed - Error: " & @error & @CRLF)
                $hWinEventProc = DllCallbackRegister("_WinEventProc", "none", "hwnd;int;hwnd;long;long;int;int")
                If Not $hWinEventProc Then
                    ConsoleWrite("DllCallbackRegister() failed - Error: " & @error & @CRLF)
                    DllClose($hDLL)
                    $hDLL = 0
                    Return 0
                EndIf
                $hHook = _SetWinEventHook($EVENT_Min, $EVENT_Max, $hDLL)
                If Not $hHook Then
                    ConsoleWrite("_SetWinEventHook failed - Error: " & @error & @CRLF)
                    DllCallbackFree($hWinEventProc)
                    DllClose($hDLL)
                    $hDLL = 0
                EndIf
                ConsoleWrite("+ Dialog Monitoring - Started" & @CRLF)
                Beep(2000, 5)
            Else ;; Stop dialog monitoring
                DllCallbackFree($hWinEventProc)
                DllCall($hDLL, "int", "UnhookWinEvent", "hwnd", $hHook)
                DllClose($hDLL)
                $hDLL = 0
                Beep(1000, 5)
                ConsoleWrite("! Dialog Monitoring - Stopped" & @CRLF)
            EndIf
        Case "{ESC}"
            Exit
    EndSwitch
EndFunc   ;==>_HotKey

Func OnAutoItExit()
    If $hWinEventProc Then
        Beep(3000, 5)
        DllCallbackFree($hWinEventProc)
        ConsoleWrite("! Dialog Monitoring - Stopped")
    EndIf
    If $hHook Then DllCall("User32.dll", "int", "UnhookWinEvent", "hwnd", $hHook)
    If $hDLL Then DllClose($hDLL)
EndFunc   ;==>OnAutoItExit

I don't know if I can adapt it to my needs, I'm not good enought at the moment.

Is there a way (by DLL call or Hooking), to capture the "EVENT_SYSTEM_MOVESIZESTART" ?

Any help from a geek would be highly appreciated :idea:

Jeremie

Share this post


Link to post
Share on other sites

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Well, I finaly got this event working :

#include <WindowsConstants.au3>
#include <WinAPI.au3>
#include <Process.au3>
#include <Misc.au3>

Global $hDLL, $hWinEventProc, $hHook

HotKeySet("{ESC}", "_HotKey") ; Exit

$hDLL = DllOpen("User32.dll")
$hWinEventProc = DllCallbackRegister("_WinEventProc", "none", "hwnd;int;hwnd;long;long;int;int")
$hHook = _SetWinEventHook(10, 11, $hDLL)

Func _WinEventProc($hHook, $iEvent, $hWnd, $idObject, $idChild, $iEventThread, $iEventTime)
Local $PID = WinGetProcess($hWnd), $sEventProcName = _ProcessGetName($PID)

; Your event code here
; Your event code here
; Your event code here
; Your event code here
; Your event code here

EndFunc

Func _SetWinEventHook($iEventMin, $iEventMax, $hDLLUser32)
    Local $aRet
    Local Const $WINEVENT_OUTOFCONTEXT = 0x0
    Local Const $WINEVENT_SKIPOWNPROCESS = 0x2
    If Not $hDLLUser32 Or $hDLLUser32 = -1 Then $hDLLUser32 = "User32.dll"
    $aRet = DllCall($hDLLUser32, "hwnd", "SetWinEventHook", _
            "uint", $iEventMin, _
            "uint", $iEventMax, _
            "hwnd", 0, _
            "ptr", DllCallbackGetPtr($hWinEventProc), _
            "int", 0, _
            "int", 0, _
            "uint", BitOR($WINEVENT_OUTOFCONTEXT, $WINEVENT_SKIPOWNPROCESS))

    If @error Then Return SetError(@error, 0, 0)
    Return $aRet[0]
EndFunc

Func _HotKey()
    Switch @HotKeyPressed
        Case "{ESC}"
            Exit
    EndSwitch
EndFunc

Func OnAutoItExit()
    If $hWinEventProc Then
        Beep(3000, 5)
        DllCallbackFree($hWinEventProc)
    EndIf
    If $hHook Then DllCall("User32.dll", "int", "UnhookWinEvent", "hwnd", $hHook)
    If $hDLL Then DllClose($hDLL)
EndFunc   ;==>OnAutoItExit
Edited by jerebenz

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Really nice :)... the OnAutoItExit() needs to be registered, and some example events would rock :idea:...

In above code 0x10 eq. $EVENT_SYSTEM_DIALOGSTART... isn't it more something like WinResizeStart? Check out this example, start, resize any window and watch SciTE console:

; http://www.autoitscript.com/forum/index.php?showtopic=114628&st=0&gopid=801863&#entry801863

#include <WindowsConstants.au3>
#include <WinAPI.au3>
#include <Process.au3>
#include <Misc.au3>

Global $hDLL, $hWinEventProc, $hHook

HotKeySet("{ESC}", "_HotKey") ; Exit

;Event Constants
;http://msdn.microsoft.com/en-us/library/ms697187.aspx
;http://msdn.microsoft.com/en-us/library/dd318066(VS.85).aspx
;http://mwinapi.sourceforge.net/doc/html/T_ManagedWinapi_Accessibility_AccessibleEventType.htm
Global Const $EVENT_SYSTEM_FOREGROUND = 0x3
Global Const $EVENT_SYSTEM_MENUSTART = 0x4
Global Const $EVENT_SYSTEM_MENUEND = 0x5
Global Const $EVENT_SYSTEM_MENUPOPUPSTART = 0x6
Global Const $EVENT_SYSTEM_MENUPOPUPEND = 0x7
Global Const $EVENT_SYSTEM_DIALOGSTART = 0x10 ; #32770 dialog box class
Global Const $EVENT_SYSTEM_DIALOGEND = 0x11 ; #32770 dialog box class
Global Const $EVENT_OBJECT_CREATE = 0x8000
Global Const $EVENT_OBJECT_DESTROY = 0x8001
Global Const $EVENT_OBJECT_SHOW = 0x8002
;probably best to use SYSTEM_FOREGROUND as OBJECT_CREATE will trigger far more frequently
;even with additional filtering in _WinEventProc

;minimum and maximum events
;to monitor one event type only use same event for min/max
Global $EVENT_Min = $EVENT_SYSTEM_DIALOGSTART
Global $EVENT_Max = $EVENT_SYSTEM_DIALOGEND

$hDLL = DllOpen("User32.dll")
$hWinEventProc = DllCallbackRegister("_WinEventProc", "none", "hwnd;int;hwnd;long;long;int;int")
if not @error then
    OnAutoItExitRegister("OnAutoItExit")
Else
    MsgBox(16+262144,"Error","_SetWinEventHook did not succeed.")
    Exit
endif

$hHook = _SetWinEventHook($EVENT_Min, $EVENT_Max, $hDLL)
; 10 = Win-Resizing Start, 11 = Win-Resizing Stop


while 1
    sleep(10)
WEnd

Func _WinEventProc($hHook, $iEvent, $hWnd, $idObject, $idChild, $iEventThread, $iEventTime)
    Local $PID = WinGetProcess($hWnd), $sEventProcName = _ProcessGetName($PID)
    ConsoleWrite("PID: " & $PID & @tab & "WinTitle: " & WinGetTitle($hWnd) & @tab & "EventTime: " & $iEventTime & @tab & @tab & "Event: " & $iEvent & @crlf)

    ; Your event code here
    ; Your event code here
    ; Your event code here
    ; Your event code here
    ; Your event code here

EndFunc   ;==>_WinEventProc

Func _SetWinEventHook($iEventMin, $iEventMax, $hDLLUser32)
    ;ConsoleWrite($iEventMin & @tab & VarGetType($iEventMin) & @crlf)
    ;ConsoleWrite($iEventMax & @tab & VarGetType($iEventMax) & @crlf)
    $iEventMin = Number(Hex($iEventMin))
    $iEventMax = Number(Hex($iEventMax))


    Local $aRet
    Local Const $WINEVENT_OUTOFCONTEXT = 0x0
    Local Const $WINEVENT_SKIPOWNPROCESS = 0x2
    If Not $hDLLUser32 Or $hDLLUser32 = -1 Then $hDLLUser32 = "User32.dll"
    $aRet = DllCall($hDLLUser32, "hwnd", "SetWinEventHook", _
            "uint", $iEventMin, _
            "uint", $iEventMax, _
            "hwnd", 0, _
            "ptr", DllCallbackGetPtr($hWinEventProc), _
            "int", 0, _
            "int", 0, _
            "uint", BitOR($WINEVENT_OUTOFCONTEXT, $WINEVENT_SKIPOWNPROCESS))
    If @error Then Return SetError(@error, 0, 0)
    Return $aRet[0]
EndFunc   ;==>_SetWinEventHook

Func _HotKey()
    Switch @HotKeyPressed
        Case "{ESC}"
            Exit
    EndSwitch
EndFunc   ;==>_HotKey

Func OnAutoItExit()
    If $hWinEventProc Then
        Beep(3000, 5)
        DllCallbackFree($hWinEventProc)
    EndIf
    If $hHook Then DllCall("User32.dll", "int", "UnhookWinEvent", "hwnd", $hHook)
    If $hDLL Then DllClose($hDLL)
EndFunc   ;==>OnAutoItExit
Edited by KaFu

Share this post


Link to post
Share on other sites

I'm using the Int values, not hex. So when I call :

$hHook = _SetWinEventHook(10, 11, $hDLL)

It realy means "Catpure events between EVENT_SYSTEM_MOVESIZESTART and EVENT_SYSTEM_MOVESIZEEND"

It's not like :

$hHook = _SetWinEventHook(0x10, 0x11, $hDLL)

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

I'm using the Int values, not hex.

I see, so more like this:

#include <WindowsConstants.au3>
#include <WinAPI.au3>
#include <Process.au3>
#include <Misc.au3>

Global $hDLL, $hWinEventProc, $hHook

HotKeySet("{ESC}", "_HotKey") ; Exit

;Event Constants
;http://msdn.microsoft.com/en-us/library/ms697187.aspx
;http://msdn.microsoft.com/en-us/library/dd318066(VS.85).aspx
;http://mwinapi.sourceforge.net/doc/html/T_ManagedWinapi_Accessibility_AccessibleEventType.htm
; http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/accessibility/AccConst.html
Global Const $EVENT_SYSTEM_SOUND = 0x0001 ;An MSAA event indicating that a sound was played.
Global Const $EVENT_SYSTEM_ALERT = 0x0002 ;An MSAA event indicating that an alert was generated.
Global Const $EVENT_SYSTEM_FOREGROUND = 0x0003 ;An MSAA event indicating that the foreground window changed.
Global Const $EVENT_SYSTEM_MENUSTART = 0x0004 ;An MSAA event indicating that a menu item on the menu bar was selected.
Global Const $EVENT_SYSTEM_MENUEND = 0x0005 ;An MSAA event indicating that a menu from the menu bar was closed.
Global Const $EVENT_SYSTEM_MENUPOPUPSTART = 0x0006 ;An MSAA event indicating that a pop-up menu was displayed.
Global Const $EVENT_SYSTEM_MENUPOPUPEND = 0x0007 ;An MSAA event indicating that a pop-up menu was closed.
Global Const $EVENT_SYSTEM_CAPTURESTART = 0x0008 ;An MSAA event indicating that a window has received mouse capture.
Global Const $EVENT_SYSTEM_CAPTUREEND = 0x0009 ;An MSAA event indicating that a window has lost mouse capture.
Global Const $EVENT_SYSTEM_DIALOGSTART = 0x0010 ;An MSAA event indicating that a dialog box was displayed.
Global Const $EVENT_SYSTEM_DIALOGEND = 0x0011 ;An MSAA event indicating that a dialog box was closed.
Global Const $EVENT_SYSTEM_SCROLLINGSTART = 0x0012 ;An MSAA event indicating that scrolling has started on a scroll bar.
Global Const $EVENT_SYSTEM_SCROLLINGEND = 0x0013 ;An MSAA event indicating that scrolling has ended on a scroll bar.
Global Const $EVENT_SYSTEM_SWITCHSTART = 0x0014 ;An MSAA event indicating that the user pressed ALT+TAB, which activates the switch window.
Global Const $EVENT_SYSTEM_SWITCHEND = 0x0015 ;An MSAA event indicating that the user released ALT+TAB.
Global Const $EVENT_SYSTEM_MINIMIZESTART = 0x0016 ;An MSAA event indicating that a window object is about to be minimized or maximized.
Global Const $EVENT_SYSTEM_MINIMIZEEND = 0x0017 ;An MSAA event indicating that a window object was minimized or maximized.
Global Const $EVENT_SYSTEM_MOVESIZESTART = 0x000A ;An MSAA event indicating that a window is being moved or resized.
Global Const $EVENT_SYSTEM_MOVESIZEEND = 0x000B ;An MSAA event indicating that the movement or resizing of a window is finished.
Global Const $EVENT_SYSTEM_CONTEXTHELPSTART = 0x000C ;An MSAA event indicating that a window entered context-sensitive Help mode.
Global Const $EVENT_SYSTEM_CONTEXTHELPEND = 0x000D  ;An MSAA event indicating that a window exited context-sensitive Help mode.
Global Const $EVENT_SYSTEM_DRAGDROPSTART = 0x000E ;An MSAA event indicating that an application is about to enter drag-and-drop mode.
Global Const $EVENT_SYSTEM_DRAGDROPEND = 0x000F ;An MSAA event indicating that an application is about to exit drag-and-drop mode.

; EVENT_OBJECT events are triggered quite often, handle with care...
Global Const $EVENT_OBJECT_CREATE = 0x8000 ;An MSAA event indicating that an object was created.
Global Const $EVENT_OBJECT_DESTROY = 0x8001 ;An MSAA event indicating that an object was destroyed.
Global Const $EVENT_OBJECT_SHOW = 0x8002 ;An MSAA event indicating that a hidden object is being shown.
Global Const $EVENT_OBJECT_HIDE = 0x8003 ;An MSAA event indicating that an object is being hidden.
Global Const $EVENT_OBJECT_REORDER = 0x8004 ;An MSAA event indicating that a container object has added, removed, or reordered its children.
Global Const $EVENT_OBJECT_FOCUS = 0x8005 ;An MSAA event indicating that an object has received the keyboard focus.
Global Const $EVENT_OBJECT_SELECTION = 0x8006 ;An MSAA event indicating that the selection within a container object changed.
Global Const $EVENT_OBJECT_SELECTIONADD = 0x8007 ;An MSAA event indicating that an item within a container object was added to the selection.
Global Const $EVENT_OBJECT_SELECTIONREMOVE = 0x8008 ;An MSAA event indicating that an item within a container object was removed from the selection.
Global Const $EVENT_OBJECT_SELECTIONWITHIN = 0x8009 ;An MSAA event indicating that numerous selection changes occurred within a container object.
Global Const $EVENT_OBJECT_HELPCHANGE = 0x8010 ;An MSAA event indicating that an object's MSAA Help property changed.
Global Const $EVENT_OBJECT_DEFACTIonchange = 0x8011 ;An MSAA event indicating that an object's MSAA DefaultAction property changed.
Global Const $EVENT_OBJECT_ACCELERATORCHANGE = 0x8012 ;An MSAA event indicating that an object's MSAA KeyboardShortcut property changed.
Global Const $EVENT_OBJECT_INVOKED = 0x8013 ;An MSAA event indicating that an object has been invoked; for example, the user has clicked a button.
Global Const $EVENT_OBJECT_TEXTSELECTIonchangeD = 0x8014 ;An MSAA event indicating that an object's text selection has changed.
Global Const $EVENT_OBJECT_CONTENTSCROLLED = 0x8015 ;An MSAA event indicating that the scrolling of a window object has ended.
Global Const $EVENT_OBJECT_STATECHANGE = 0x800A ;An MSAA event indicating that an object's state has changed.
Global Const $EVENT_OBJECT_LOCATIonchange = 0x800B ;An MSAA event indicating that an object has changed location, shape, or size.
Global Const $EVENT_OBJECT_NAMECHANGE = 0x800C ;An MSAA event indicating that an object's MSAA Name property changed.
Global Const $EVENT_OBJECT_DESCRIPTIonchange = 0x800D ;An MSAA event indicating that an object's MSAA Description property changed.
Global Const $EVENT_OBJECT_VALUECHANGE = 0x800E ;An MSAA event indicating that an object's MSAA Value property changed.
Global Const $EVENT_OBJECT_PARENTCHANGE = 0x800F ;An MSAA event indicating that an object has a new parent object.

;minimum and maximum events
;to monitor one event type only use same event for min/max
Global $EVENT_Min = $EVENT_SYSTEM_SOUND
Global $EVENT_Max = $EVENT_SYSTEM_DRAGDROPEND

$hDLL = DllOpen("User32.dll")
$hWinEventProc = DllCallbackRegister("_WinEventProc", "none", "hwnd;int;hwnd;long;long;int;int")
if not @error then
    OnAutoItExitRegister("OnAutoItExit")
Else
    MsgBox(16+262144,"Error","DllCallbackRegister(_WinEventProc) did not succeed.")
    Exit
endif

$hHook = _SetWinEventHook($EVENT_Min, $EVENT_Max, $hDLL)
if @error then
    MsgBox(16+262144,"Error","_SetWinEventHook() did not succeed.")
    Exit
endif

while 1
    sleep(10)
WEnd

Func _WinEventProc($hHook, $iEvent, $hWnd, $idObject, $idChild, $iEventThread, $iEventTime)
    Local $PID = WinGetProcess($hWnd), $sEventProcName = _ProcessGetName($PID)
    ConsoleWrite("PID: " & $PID & @tab & "WinTitle: " & StringLeft(WinGetTitle($hWnd),30) & @tab & "EventTime: " & $iEventTime & @tab & @tab & "Event: " & $iEvent & @crlf)

    ; Your own event handling code here...

EndFunc   ;==>_WinEventProc

Func _SetWinEventHook($iEventMin, $iEventMax, $hDLLUser32)
    ;ConsoleWrite($iEventMin & @tab & VarGetType($iEventMin) & @crlf)
    ;ConsoleWrite($iEventMax & @tab & VarGetType($iEventMax) & @crlf)

    Local $aRet
    Local Const $WINEVENT_OUTOFCONTEXT = 0x0
    Local Const $WINEVENT_SKIPOWNPROCESS = 0x2
    If Not $hDLLUser32 Or $hDLLUser32 = -1 Then $hDLLUser32 = "User32.dll"
    $aRet = DllCall($hDLLUser32, "hwnd", "SetWinEventHook", _
            "uint", $iEventMin, _
            "uint", $iEventMax, _
            "hwnd", 0, _
            "ptr", DllCallbackGetPtr($hWinEventProc), _
            "int", 0, _
            "int", 0, _
            "uint", BitOR($WINEVENT_OUTOFCONTEXT, $WINEVENT_SKIPOWNPROCESS))
    If @error Then Return SetError(@error, 0, 0)
    Return $aRet[0]
EndFunc   ;==>_SetWinEventHook

Func _HotKey()
    Switch @HotKeyPressed
        Case "{ESC}"
            Exit
    EndSwitch
EndFunc   ;==>_HotKey

Func OnAutoItExit()
    If $hWinEventProc Then
        Beep(3000, 5)
        DllCallbackFree($hWinEventProc)
    EndIf
    If $hHook Then DllCall("User32.dll", "int", "UnhookWinEvent", "hwnd", $hHook)
    If $hDLL Then DllClose($hDLL)
EndFunc   ;==>OnAutoItExit

Edit: Hu, I just realize that the EVENT_OBJECTS mentioned in http://msdn.microsoft.com/en-us/library/ms697187.aspx are partially controls (e.g. change of edit control content). This definitely needs some more investigation :idea:...

Edited by KaFu

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now