green3 Posted May 28, 2020 Share Posted May 28, 2020 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 More sharing options...
Musashi Posted May 28, 2020 Share Posted May 28, 2020 (edited) 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 May 28, 2020 by Musashi Example extended "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 More sharing options...
Nine Posted May 28, 2020 Share Posted May 28, 2020 You may want to take a look at _Timer_GetIdleTime ( ). Checks both mouse and KB. “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
green3 Posted May 28, 2020 Author Share Posted May 28, 2020 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 More sharing options...
Nine Posted May 28, 2020 Share Posted May 28, 2020 (edited) 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 May 28, 2020 by Nine Musashi 1 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
green3 Posted May 28, 2020 Author Share Posted May 28, 2020 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 More sharing options...
Musashi Posted May 28, 2020 Share Posted May 28, 2020 22 minutes ago, Nine said: I will let @Musashi solve his "issue". Thanks . Damn, my script opens instances of Notepad in millisecond intervals . 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 . "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 More sharing options...
green3 Posted May 28, 2020 Author Share Posted May 28, 2020 13 minutes ago, Musashi said: Thanks . Damn, my script opens instances of Notepad in millisecond intervals . 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 More sharing options...
Musashi Posted May 28, 2020 Share Posted May 28, 2020 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 "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 More sharing options...
green3 Posted May 28, 2020 Author Share Posted May 28, 2020 Thanks. Link to comment Share on other sites More sharing options...
rudi Posted June 8, 2020 Share Posted June 8, 2020 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. expandcollapse popup#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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now