#include-once Global $__iStdMemAlloc = 1024 Global $__GlobalEvents_[$__iStdMemAlloc][4] Global $__iEventDelay = 333 Global Enum $__sEventEval, $__sEventHandler, $__iEventFlag _GlobalEvents_Start() ; #FUNCTION# ==================================================================================================================== ; Name ..........: _GlobalEvents_Destroy ; Description ...: Destroy a previously created event. ; Syntax ........: _GlobalEvents_Destroy($iEventID) ; Parameters ....: $iEventID - the event ID to destroy (returned by _GlobalEvents_Create). ; Return values .: None ; Author ........: minxomat ; Remarks .......: Not required for single-execution events. Useful to stop looping events. ; =============================================================================================================================== Func _GlobalEvents_Destroy($iEventID) $__GlobalEvents_[$iEventID][0] = "" EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _GlobalEvents_SetDelay ; Description ...: Set the delay between each event loop. ; Syntax ........: _GlobalEvents_SetDelay($iDelay) ; Parameters ....: $iDelay - Delay in ms (default is 333ms). ; Author ........: minxomat ; =============================================================================================================================== Func _GlobalEvents_SetDelay($iDelay) _GlobalEvents_Pause() $__iEventDelay = $iDelay _GlobalEvents_Start() EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _GlobalEvents_Start ; Description ...: Re-Start the event loop. ; Syntax ........: _GlobalEvents_Start() ; Return values .: Passes the Adlib return value. ; Author ........: minxomat ; Remarks .......: Only call this after _GlobalEvents_Pause! ; =============================================================================================================================== Func _GlobalEvents_Start() Return AdlibRegister("_GlobalEvents_Loop", $__iEventDelay) EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _GlobalEvents_Pause ; Description ...: Pauses the event loop. No event will be triggered. ; Syntax ........: _GlobalEvents_Pause() ; Return values .: Passes the Adlib return value. ; Author ........: minxomat ; Remarks .......: Call _GlobalEvents_Start to restart the loop (from the beginning).. ; =============================================================================================================================== Func _GlobalEvents_Pause() Return AdlibUnRegister("_GlobalEvents_Loop") EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _GlobalEvents_Loop ; Description ...: Evaluate events and call their handler functions (with arguments if supplied). ; Syntax ........: _GlobalEvents_Loop() ; Author ........: minxomat ; Remarks .......: Do not manually call this! Use _GlobalEvents_Start and _GlobalEvents_Pause to control the execution. ; =============================================================================================================================== Func _GlobalEvents_Loop() If $__GlobalEvents_[0][0] = "" Then Return For $nEvent = 0 To UBound($__GlobalEvents_)-1 If $__GlobalEvents_[$nEvent][0] = "" Or $__GlobalEvents_[$nEvent][2] = 1 Then ContinueLoop If Execute($__GlobalEvents_[$nEvent][0]) Then If $__GlobalEvents_[$nEvent][2] = 0 Or $__GlobalEvents_[$nEvent][2] = 2 Then If $__GlobalEvents_[$nEvent][3] <> 0 Then $__GlobalEvents_[$nEvent][1]($__GlobalEvents_[$nEvent][3]) Else $__GlobalEvents_[$nEvent][1]() EndIf If $__GlobalEvents_[$nEvent][2] = 0 Then $__GlobalEvents_[$nEvent][2] = 1 EndIf EndIf Next EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _GlobalEvents_ResetEvent ; Description ...: Resets single-execution events. ; Syntax ........: _GlobalEvents_ResetEvent($iEventID) ; Parameters ....: $iEventID - Event ID. ; Return values .: 1 if reset was successful, -1 if the event is not compatible with this function. ; Author ........: minxomat ; =============================================================================================================================== Func _GlobalEvents_ResetEvent($iEventID) If $__GlobalEvents_[$iEventID][2] = 2 Then Return -1 $__GlobalEvents_[$iEventID][2] = 0 Return 1 EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _GlobalEvents_Create ; Description ...: Create an event. ; Syntax ........: _GlobalEvents_Create($sEventEval, $pEventHandler[, $iEventFlag = 0[, $aArguments = 0]]) ; Parameters ....: $sEventEval - The AutoIt single-line expression to evaluate. ; $pEventHandler - Pointer to event handler. ; $iEventFlag - [optional] Execution flag: 0 - Triggers event only once. (see _GlobalEvents_ResetEvent) ; 1 - RESERVED ; 2 - Trigger every event loop until - eval becomes FALSE or ; - event is destroyed. ; $aArguments - [optional] an array of parameters to pass to the handler. Default is no paramters (0). ; Return values .: Event ID / Handle. Save this if you want to destroy or reset the event! ; Author ........: minxomat ; =============================================================================================================================== Func _GlobalEvents_Create($sEventEval, $pEventHandler, $iEventFlag = 0, $aArguments = 0) Local $iNewEventID = __GlobalEvents_FindSlot() If $iNewEventID = -1 Then $iNewEventID = $__iStdMemAlloc $__iStdMemAlloc += 1024 ReDim $__GlobalEvents_[$__iStdMemAlloc][4] EndIf $__GlobalEvents_[$iNewEventID][0] = $sEventEval $__GlobalEvents_[$iNewEventID][1] = $pEventHandler $__GlobalEvents_[$iNewEventID][2] = $iEventFlag $__GlobalEvents_[$iNewEventID][3] = $aArguments Return $iNewEventID EndFunc ; internals Func __GlobalEvents_FindSlot() For $nSlot = 0 To UBound($__GlobalEvents_)-1 If $__GlobalEvents_[$nSlot][0] = "" Then Return $nSlot Next Return -1 EndFunc