Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <WinAPISys.au3>
- Global Const $IDLETIME_GUID = '5816AA22-EEB4-4C92-BB07-4A5E1DBA4A6A'
- Global Enum $IDLETIME_DELEGATE, $IDLETIME_ID, $IDLETIME_ISRUNNING, $IDLETIME_TIME, $IDLETIME_MAX
- Global $g_bIsRunning = True ; For the example only. This is set to false when ESC is pressed.
- HotKeySet('{ESC}', Close)
- Example()
- Func Example()
- Local $hIdle = _IdleTime(10000, WakeUp) ; Create an idle time object and set the time to move the mouse every 10 seconds and call the parameterless function WakeUp().
- ConsoleWrite('IsRunning: ' & _IdleTime_IsRunning($hIdle) & @CRLF) ; Display running status.
- ConsoleWrite('Time: ' & _IdleTime_GetTime($hIdle) & @CRLF)
- _IdleTime_StopStartPause($hIdle) ; Start the idle time monitoring.
- ConsoleWrite('IsRunning: ' & _IdleTime_IsRunning($hIdle) & @CRLF) ; Display running status.
- While Sleep(250) And $g_bIsRunning
- WEnd
- If _IdleTime_IsRunning($hIdle) Then
- _IdleTime_StopStartPause($hIdle) ; Stop if the idle time is currently running.
- EndIf
- ConsoleWrite('IsRunning: ' & _IdleTime_IsRunning($hIdle) & @CRLF)
- Return True
- EndFunc ;==>Example
- Func Close()
- $g_bIsRunning = False
- EndFunc ;==>Close
- Func WakeUp()
- MsgBox($MB_SYSTEMMODAL, '', 'Please wake up!')
- EndFunc ;==>WakeUp
- #Region IdleTime UDF
- Func _IdleTime($iTime = Default, $hFunc = Default)
- Local $aIdleTime[$IDLETIME_MAX]
- $aIdleTime[$IDLETIME_ID] = $IDLETIME_GUID
- $aIdleTime[$IDLETIME_ISRUNNING] = False
- __IdleTime_Delegate($aIdleTime, $hFunc) ; Set the delegate function. This should have no parameters.
- __IdleTime_Time($aIdleTime, $iTime) ; Set the time.
- Return $aIdleTime
- EndFunc ;==>_IdleTime
- Func _IdleTime_GetDelegate(ByRef $aIdleTime)
- Return (__IdleTime_IsAPI($aIdleTime) ? $aIdleTime[$IDLETIME_DELEGATE] : Null)
- EndFunc ;==>_IdleTime_GetDelegate
- Func _IdleTime_GetTime(ByRef $aIdleTime)
- Return (__IdleTime_IsAPI($aIdleTime) ? $aIdleTime[$IDLETIME_TIME] : Null)
- EndFunc ;==>_IdleTime_GetTime
- Func _IdleTime_IsRunning(ByRef $aIdleTime)
- Return (__IdleTime_IsAPI($aIdleTime) ? $aIdleTime[$IDLETIME_ISRUNNING] : False)
- EndFunc ;==>_IdleTime_IsRunning
- Func _IdleTime_SetDelegate(ByRef $aIdleTime, $hFunc)
- Local $bReturn = False
- If __IdleTime_IsAPI($aIdleTime) And __IdleTime_Time($aIdleTime, $hFunc) Then ; Set the delegate.
- $bReturn = True
- If _IdleTime_IsRunning($aIdleTime) Then
- _IdleTime_StopStartPause($aIdleTime) ; Stop.
- _IdleTime_StopStartPause($aIdleTime) ; Start.
- EndIf
- EndIf
- Return $bReturn
- EndFunc ;==>_IdleTime_SetDelegate
- Func _IdleTime_SetTime(ByRef $aIdleTime, $iTime)
- Local $bReturn = False
- If __IdleTime_IsAPI($aIdleTime) And __IdleTime_Time($aIdleTime, $iTime) Then ; Set the time.
- $bReturn = True
- If _IdleTime_IsRunning($aIdleTime) Then
- _IdleTime_StopStartPause($aIdleTime) ; Stop.
- _IdleTime_StopStartPause($aIdleTime) ; Start.
- EndIf
- EndIf
- Return $bReturn
- EndFunc ;==>_IdleTime_SetTime
- Func _IdleTime_StopStartPause(ByRef $aIdleTime)
- Local $bReturn = False
- If __IdleTime_IsAPI($aIdleTime) Then
- $bReturn = True
- If $aIdleTime[$IDLETIME_ISRUNNING] Then
- __IdleTime_Proc(0, 0) ; Set the static variablse in the procedure to the default of zero, thus clearing the previous values.
- AdlibUnRegister(__IdleTime_AdLibRegister)
- Else
- __IdleTime_Proc($aIdleTime[$IDLETIME_DELEGATE], $aIdleTime[$IDLETIME_TIME]) ; Set the static variables in the procedure to the required time when to move the mouse and delegate to call.
- AdlibRegister(__IdleTime_AdLibRegister, Ceiling($aIdleTime[$IDLETIME_TIME] / 3)) ; Register the function to be called time / 3.
- EndIf
- $aIdleTime[$IDLETIME_ISRUNNING] = Not $aIdleTime[$IDLETIME_ISRUNNING]
- EndIf
- Return $bReturn
- EndFunc ;==>_IdleTime_StopStartPause
- Func __IdleTime_IsAPI(ByRef $aIdleTime)
- Return UBound($aIdleTime) = $IDLETIME_MAX And $aIdleTime[$IDLETIME_ID] = $IDLETIME_GUID
- EndFunc ;==>__IdleTime_IsAPI
- Func __IdleTime_AdLibRegister() ; Wrapper for __IdleTime_Proc(), since AdLibRegister() doesn't accept functions with parameters.
- Return __IdleTime_Proc()
- EndFunc ;==>__IdleTime_AdLibRegister
- Func __IdleTime_Delegate(ByRef $aIdleTime, $hFunc)
- If Not IsFunc($hFunc) Then $hFunc = 0
- $aIdleTime[$IDLETIME_DELEGATE] = $hFunc
- Return True
- EndFunc ;==>__IdleTime_Delegate
- Func __IdleTime_Proc($hSetFunc = Default, $iSetTime = Default)
- Local Static $hFunc = 0, _
- $iTime = 0
- If $hSetFunc = Default And $iSetTime = Default And $iTime > 0 Then
- If _WinAPI_GetIdleTime() >= $iTime Then
- Local $aPos = MouseGetPos()
- If Not @error Then
- Local Enum $POS_X, $POS_Y
- MouseMove($aPos[$POS_X] + 1, $aPos[$POS_Y])
- MouseMove($aPos[$POS_X], $aPos[$POS_Y])
- If IsFunc($hFunc) Then ; Call a function if it's set.
- $hFunc()
- EndIf
- EndIf
- EndIf
- Else
- If Not ($hSetFunc = Default) Then $hFunc = $hSetFunc
- If Not ($iSetTime = Default) Then $iTime = $iSetTime
- EndIf
- Return True
- EndFunc ;==>__IdleTime_Proc
- Func __IdleTime_Time(ByRef $aIdleTime, $iTime)
- If $iTime = Default Or $iTime < 750 Then $iTime = 750 ; 750 ms by default, since the procedure is checked 750 / 3 = 250 (which is the minimum for AdLibRegister()).
- $aIdleTime[$IDLETIME_TIME] = $iTime
- Return True
- EndFunc ;==>__IdleTime_Time
- #EndRegion IdleTime UDF
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement