Jump to content

Simple Idle timer help please.


Recommended Posts

I'm trying to make a simple idle timer, if no mouse movement for 2 minutes then mouse click left and shell execute a program. If there is mouse movement restart the timer. I've searched the forums but haven't found what I'm looking for. Thank you in advance!

Link to comment
Share on other sites

3 hours ago, green3 said:

I've searched the forums but haven't found what I'm looking for.

There are a lot of examples that you can easily find with Google, for example ;):

#include <AutoItConstants.au3>

Global $iLastMousePos  = MouseGetPos()
Global $hLastMouseMove = TimerInit()
Global $iCurMousePos

HotKeySet("{ESC}", "_Exit")

While True
    $iCurMousePos = MouseGetPos()
    If ( $iLastMousePos[0] == $iCurMousePos[0] And $iLastMousePos[1] == $iCurMousePos[1] ) Then
        If ( TimerDiff($hLastMouseMove) > 10000 ) Then ; 10,000 ms = 10 seconds
            MsgBox(0, "", "Mouse hasn't moved for 10 seconds.")

            MouseClick($MOUSE_CLICK_LEFT)
            ShellExecute('Notepad.exe')
            ExitLoop
            
        EndIf
    Else
        $iLastMousePos  = $iCurMousePos
        $hLastMouseMove = TimerInit()
    EndIf
    Sleep(10)
WEnd

Func _Exit()
    ConsoleWrite('! -> _Exit() ==> EXIT' & @CRLF)
    Exit
EndFunc   ;==>_Exit

EDIT : @green3 -> Example extended

EDIT 2 : Insert ExitLoop

Edited by Musashi
Example extended

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

51 minutes ago, Nine said:

You may want to take a look at _Timer_GetIdleTime ( ).  Checks both mouse and KB.

 

1 hour ago, Musashi said:

There are a lot of examples that you can easily find with Google, for example ;):

#include <AutoItConstants.au3>

Global $iLastMousePos  = MouseGetPos()
Global $hLastMouseMove = TimerInit()
Global $iCurMousePos

HotKeySet("{ESC}", "_Exit")

While True
    $iCurMousePos = MouseGetPos()
    If ( $iLastMousePos[0] == $iCurMousePos[0] And $iLastMousePos[1] == $iCurMousePos[1] ) Then
        If ( TimerDiff($hLastMouseMove) > 10000 ) Then ; 10,000 ms = 10 seconds
            MsgBox(0, "", "Mouse hasn't moved for 10 seconds.")

            MouseClick($MOUSE_CLICK_LEFT)
            ShellExecute('Notepad.exe')
            
        EndIf
    Else
        $iLastMousePos  = $iCurMousePos
        $hLastMouseMove = TimerInit()
    EndIf
    Sleep(10)
WEnd

Func _Exit()
    ConsoleWrite('! -> _Exit() ==> EXIT' & @CRLF)
    Exit
EndFunc   ;==>_Exit

EDIT : @green3 -> Example extended

Thank you for responding, The problem is that while it does mouseclickleft, it never stops mouseclicking left vs click once then restart the timer.

 

#include <AutoItConstants.au3>

Global $iLastMousePos  = MouseGetPos()
Global $hLastMouseMove = TimerInit()
Global $iCurMousePos

HotKeySet("{ESC}", "_Exit")

While True
    $iCurMousePos = MouseGetPos()
    If ( $iLastMousePos[0] == $iCurMousePos[0] And $iLastMousePos[1] == $iCurMousePos[1] ) Then
        If ( TimerDiff($hLastMouseMove) > 10000 ) Then ; 10,000 ms = 10 seconds
            ;MsgBox(0, "", "Mouse hasn't moved for 10 seconds.")

            MouseClick($MOUSE_CLICK_LEFT)
            ;ShellExecute('Notepad.exe')
            
        EndIf
    Else
        $iLastMousePos  = $iCurMousePos
        $hLastMouseMove = TimerInit()
    EndIf
    Sleep(10)
WEnd

Func _Exit()
    ConsoleWrite('! -> _Exit() ==> EXIT' & @CRLF)
    Exit
EndFunc   ;==>_Exit
Link to comment
Share on other sites

I will let @Musashi solve his "issue".  But here an example of what I proposed earlier :

#include <Timers.au3>
#include <Constants.au3>

Global Const $DELAY = 25000

HotKeySet("{ESC}", _Exit)

While True
  If _Timer_GetIdleTime() > $DELAY Then
    MouseClick($MOUSE_CLICK_LEFT)
    ShellExecute('Notepad.exe')
  EndIf
  Sleep (100)
WEnd

Func _Exit()
  Exit
EndFunc

 

Edited by Nine
Link to comment
Share on other sites

14 minutes ago, Nine said:

I will let @Musashi solve his "issue".  But here an example of what I proposed earlier :

#include <Timers.au3>
#include <Constants.au3>

Global Const $DELAY = 25000

HotKeySet("{ESC}", _Exit)

While True
  If _Timer_GetIdleTime() > $DELAY Then
    MouseClick($MOUSE_CLICK_LEFT)
    ShellExecute('Notepad.exe')
  EndIf
  Sleep (100)
WEnd

Func _Exit()
  Exit
EndFunc

 

Resolved, Thank you so much!!!!

Link to comment
Share on other sites

22 minutes ago, Nine said:

I will let @Musashi solve his "issue".

Thanks :lol:.

Damn, my script opens instances of Notepad in millisecond intervals :mad2:. At least ExitLoop must be implemented after ShellExecute().

@Nine : But this also happens with your script, even if only every $DELAY = 25000 (milliseconds).

_Timer_GetIdleTime seems to be the appropriate function for me as well. In the start post, however, only mouse movements were mentioned, while _Timer_GetIdleTime also reacts to keyboard input. But since @green3 seems to be satisfied with your solution, I leave it that way ;).

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

13 minutes ago, Musashi said:

Thanks :lol:.

Damn, my script opens instances of Notepad in millisecond intervals :mad2:. At least ExitLoop must be implemented after ShellExecute().

@Nine : But this also happens with your script, even if only every $DELAY = 25000 (milliseconds).

_Timer_GetIdleTime seems to be the appropriate function for me as well. In the start post, however, only mouse movements were mentioned, while _Timer_GetIdleTime also reacts to keyboard input. But since @green3 seems to be satisfied with your solution, I leave it that way ;).

@Musashi, It's actually exactly what I was looking for. How would you implement exitloop on it though, just under sleep (100)?

Link to comment
Share on other sites

3 minutes ago, green3 said:

How would you implement exitloop on it though, just under sleep (100)?

If you want to terminate the script/loop after ShellExecute, then :

#include <Timers.au3>
#include <Constants.au3>

Global Const $DELAY = 25000

HotKeySet("{ESC}", _Exit)

While True
  If _Timer_GetIdleTime() > $DELAY Then
    MouseClick($MOUSE_CLICK_LEFT)
    ShellExecute('Notepad.exe')
    ExitLoop
  EndIf
  Sleep (100)
WEnd

Func _Exit()
  Exit
EndFunc

 

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

  • 2 weeks later...

For an meanwhile obsolete Xerox tool "QCONTROL" (printing processing) I faced the issue, that opening the program's config system will make the QCONTOL system to pause.

 

So this was the code closing the config GUI in case it was unintendedly open after adjusting the configuration, and when the main program vanished (faulty close, or crash), it was restarted automatically. This script is from 2009, so for sure it's compiled with a very outdated version of Autoit!

For reading the German language strings pls. use google translate, if required.

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Res_Fileversion=0.0.0.3
#AutoIt3Wrapper_Res_Fileversion_AutoIncrement=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <date.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Opt("Trayiconhide", 1)


$ProgPath = "c:\programme\xerox\Docuwide Qseries\Xerox QControl\QControl.exe"
$Process = "QControl.exe"
$logfile = "QControl.log"

$ConfigTimeOut=5

$w=350
$h=50
$InfoBox=guicreate("",$w,$h,@DesktopWidth-$w-20,20,$WS_POPUP)
$InfoTxt="Config Dialog ist offen -> Spool Betrieb ist jetzt blockiert!" & @lf & "Nach " & $ConfigTimeOut & " Min. ohne Aktivität wird er mit ""CANCEL"" geschlossen."
$Label=GUICtrlCreateLabel($InfoTxt,1,1,$w,$h)
GUICtrlSetBkColor($Label,0x80ff80)
GUISetState()
GUISetState(@SW_HIDE)


AdlibEnable("CloseConfigOnIdle",5000)

While 1
    If Not ProcessExists($Process) Then
        Run($ProgPath)
        FileWriteLine($logfile, _NowCalc() & ": Prozess " & $Process & " fehlte. Programm neu gestartet.")
        Sleep(10000)
    EndIf
    Sleep(100)
WEnd


Func CloseConfigOnIdle()
    if WinExists("Settings","Apply") Then
        dbg("Config ist offen")
        GUISetState(@SW_SHOW,$InfoBox)
        WinSetOnTop($InfoBox,"",1)
        $idle=_Timer_GetIdleTime()
        $rest=$ConfigTimeOut*60-$idle
        dbg($rest)
        if $rest < 60 then 
            GUICtrlSetBkColor($Label,0xff0000)
        elseif $rest < 180 then 
            GUICtrlSetBkColor($Label,0xffff70)
        Else
            GUICtrlSetBkColor($Label,0x70ff70)
        EndIf

        GUICtrlSetData($Label,$InfoTxt & @lf & "Noch " & $rest & " Sekunden, dann wird der Config Dialog ge-QUIT-tet!")
        if _Timer_GetIdleTime() / 60 > $ConfigTimeOut Then
            $res=ControlClick("Settings","Apply","ThunderRT6CommandButton3") ; Cancel Button
            ConsoleWrite("Result Click: " & $res & @CRLF)
        EndIf
    Else
        GUISetState(@SW_HIDE,$InfoBox)
    EndIf
EndFunc


;
; credits go to 
; Karsten Violka (kav@ctmagazin.de), Stefan Porteck (spo@ctmagazin.de)
;
Func Dbg($msg, $error = @error, $extended = @extended, $ScriptLineNumber = @ScriptLineNumber)
    
    ; view with debug view 
    ; http://technet.microsoft.com/en-us/sysinternals/bb896647.aspx
    
    Local $out = $msg & ", Line:" & $ScriptLineNumber & ", Error: " & $error & " " & $extended

    ;Output to application attaching a console to the script engine
    ConsoleWrite($out & @CRLF)

    ;Output to debugger (dbgview.exe)
    DllCall("kernel32.dll", "none", "OutputDebugString", "str", $out)
        
EndFunc   ;==>_DebugPrint

        
        
        
        
        
        
        
        ; #FUNCTION#;===============================================================================
;
; Name...........: _Timer_GetIdleTime()
; Description ...: Returns the number of ticks since last user activity (i.e. KYBD/Mouse)
; Syntax.........: _Timer_GetIdleTime()
; Parameters ....: None
; Return values .: Success - integer ticks since last (approx. milliseconds) since last activity
;                 Failure - Sets @extended = 1 if rollover occurs (see remarks)
; Author ........: PsaltyDS at http://www.autoitscript.com/forum
; Modified.......: v1.0.0  --  03/20/2008 First version
;                 v1.0.1  --  06/11/2008 Fixed bug when idle time = 0ms
; Remarks .......: The current ticks since last system restart will roll over to 0 every 50 days or so,
;                 which makes it possible for last user activity to be before the rollover, but run time
;                 of this function to be after the rollover.  If this happens, @extended = 1 and the
;                 returned value is ticks since rollover occured.
; Related .......: _CheckIdle()
; Link ..........;
; Example .......; Yes
;
;;==========================================================================================


Func _Timer_GetIdleTime()
; Get ticks at last activity
    Local $tStruct = DllStructCreate("uint;dword");
    DllStructSetData($tStruct, 1, DllStructGetSize($tStruct));
    DllCall("user32.dll", "int", "GetLastInputInfo", "ptr", DllStructGetPtr($tStruct))

; Get current ticks since last restart
    Local $avTicks = DllCall("Kernel32.dll", "int", "GetTickCount")

; Return time since last activity, in ticks (approx milliseconds)
    Local $iDiff = $avTicks[0] - DllStructGetData($tStruct, 2)
    If $iDiff >= 0 Then
    ; Normal return
        Return floor($iDiff/1000)
    Else
    ; Rollover of ticks counter has occured
        Return SetError(0, 1, $avTicks[0])
    EndIf
EndFunc  ;==>_Timer_GetIdleTime

 

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...