Jump to content

Retrieve idle time function


Recommended Posts

Hi,

I've been trying to make a function that returns the system idle time (the amount of time the system is inactive - mouse movement) in a format of "X hours Y minutes Z seconds", the function is a part of a bigger script so when i use try capturing the idle time using a while loop that increases the number of seconds according to the mouse x,y the script ignores everything i do in the bigger script because of the while + sleep loop..

i've also tried to use _Date_Time_GetSystemTimes() (see syntrax in the help file and the example script) and all i get is a strange date that has nothing to do with the idle time..

can somebody help me make a function that retrieves the idle time in the specified format without using a while loop to capture the idle time or if possible, use while loop and still be able to excecute commands from the bigger script..

thnx in advance :)

Link to comment
Share on other sites

2, this script will check if mouse have moved

#NoTrayIcon
$pos = MouseGetPos()
$X=$pos[0] 
$Y=$pos[1]

msgbox(64,"",$X & ", " &  $Y)


$begin = TimerInit()
sleep(5000)
$dif = TimerDiff($begin)

$pos1 = MouseGetPos()
$X1=$pos1[0] 
$Y1=$pos1[1]


if $X<>$X1 or $Y<>$Y1 Then
    MsgBox(64,"moved","You have moved the mouse")
Else
    MsgBox(64,"Not moved","No Clicked")
EndIf

the next prob,have a look at TimerInit ( ) and TimerDiff() , also search the forum , i remember reading a topic show how to do something similar to your question

[quote]Don't expect for a perfect life ... Expect a least troubles ones[/quote]Contact me : ass@kiss.toWhat I Have Done :Favorites Manager Mangage your favorite's folder, that's coolPC Waker For those who want to save stickersWebScipts Supporter For those who've just started with Web and WebScriptsTemporary Looker Simple but powerful to manage your Temporary folder, you know what you downloaded[UDF] _NumberFormat() Better performance on number display[UDF] _DirGet() What a folder contain [how many (hidden,normal,...) files], with one line of code[UDF] _IsPressEs() Just like _IsPress() but for a group of keys

Link to comment
Share on other sites

Hi,

I've been trying to make a function that returns the system idle time (the amount of time the system is inactive - mouse movement) in a format of "X hours Y minutes Z seconds", the function is a part of a bigger script so when i use try capturing the idle time using a while loop that increases the number of seconds according to the mouse x,y the script ignores everything i do in the bigger script because of the while + sleep loop..

i've also tried to use _Date_Time_GetSystemTimes() (see syntrax in the help file and the example script) and all i get is a strange date that has nothing to do with the idle time..

can somebody help me make a function that retrieves the idle time in the specified format without using a while loop to capture the idle time or if possible, use while loop and still be able to excecute commands from the bigger script..

thnx in advance :)

Larry did a modification of GaryFrost's code that works well without hogging cpu. His original post was deleted from the thread, but is quoted in this post:

http://www.autoitscript.com/forum/index.ph...st&p=343430

Link to comment
Share on other sites

This is my function (which may have been done better elsewhere). It returns the time in ticks (milliseconds, approximately) since the last user input:

#include <date.au3> ; For _TicksToTime() only

Global $iIdleTime = _Timer_GetIdleTime()
ConsoleWrite("Debug: Idle time = " & $iIdleTime & "ms" & @LF)

Sleep(10 * 1000) ; 10sec

$iIdleTime = _Timer_GetIdleTime()
ConsoleWrite("Debug: Idle time = " & $iIdleTime & "ms" & @LF)

Global $Hr, $Min, $Sec
_TicksToTime($iIdleTime, $Hr, $Min, $Sec)
ConsoleWrite("Debug: Converted = " & $Hr & ":" & $Min & ":" & $Sec & @LF)

; ================================================
; function:  _Timer_GetIdleTime()
; purpose:  Returns the number of ticks since last user activity (i.e. KYBD/Mouse)
; syntax:  _Timer_GetIdleTime()
; returns:  On success:  integer ticks since last (approx. milliseconds) since last activity
; notes:  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.
; author:  PsaltyDS at www.autoitscript.com/forum
; ================================================
; Change log:
; v1.0.0  --  03/20/2008 First version
; ================================================
Func _Timer_GetIdleTime()
    ; Get ticks at last activity
    Local $struct = DllStructCreate("uint;dword");
    DllStructSetData($struct, 1, DllStructGetSize($struct));
    DllCall("user32.dll", "int", "GetLastInputInfo", "ptr", DllStructGetPtr($struct))
    
    ; Get current ticks since last restart
    Local $aTicks = DllCall("Kernel32.dll", "int", "GetTickCount")

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

I got the first DLL call from Gary, the second is straight off _Date_Time_GetTickCount().

Cheers.

:)

P.S. Just use _TicksToTime() if you want Hrs/Mins/Secs from that (demo updated to show that).

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
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...