udfGetIdleTime
int/arr udfGetIdleTime (int)
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfGetIdleTime (intMode)
intTicksSystemUp = GetTickCount () ; Get system uptime.
intTicksLastInput = 0              ; The tick at which the last input was detected.
intTicksIdle = 0                   ; The number of ticks that passed since last input.

; "GetLastInputInfo" provides session-specific user input information for only the session that invoked the function.
hdlBBLastInputInfo = BinaryAlloc (8)   ; Set the LASTINPUTINFO structure.
BinaryPoke4 (hdlBBLastInputInfo, 0, 8) ; cbSize ; The size of the structure, in bytes.
BinaryPoke4 (hdlBBLastInputInfo, 4, 0) ; dwTime ; The tick count when the last input event was received.
intResult = DllCall (DirWindows (1) : "user32.dll", long : "GetLastInputInfo", lpbinary : hdlBBLastInputInfo)
If intResult
   intTicksLastInput = BinaryPeek4 (hdlBBLastInputInfo, 4) ; Get the number of ticks at the point when the last activity was detected.
   intTicksIdle = intTicksSystemUp - intTicksLastInput     ; Number of idle ticks = system uptime ticks - number of ticks at last input.
EndIf
hdlBBLastInputInfo = BinaryFree (hdlBBLastInputInfo)

; Convert milliseconds to seconds. Rounded to seconds.
If !intMode
   Return Int (intTicksIdle / 1000.0)
Else
   intSecSystemUptime = Int (intTicksSystemUp / 1000.0)
   intSecLastInput = Int (intTicksLastInput / 1000.0)
   intSecIdle = Int (intTicksIdle / 1000.0)
   Return Arrayize (intSecIdle : "," : intSecLastInput : "," : intSecSystemUptime, ",")
EndIf
;..........................................................................................................................................
; This UDF "udfGetIdleTime" returns the idle time in seconds between the last activity of keyboard or mouse until now.
;
; Parameter:
; intMode = 0 ... Return one integer value as seconds of idle time.
; intMode = 1 ... Return array of integer values as seconds of idle time, last input time, system up time.
;
; Code proposal from ...
; Topic:  Check for no activity
; Conf:  WinBatch
; From:  mdlines martin@martinlines.com
; Date:  Wednesday, January 18, 2012 04:42 AM
;
; (c)Detlev Dalitz.20120119.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


; Test.

DirChange (DirScript ())

strMsgTitle = "Test udfGetIdleTime"
strMsgText = "Starting test ..." : @LF : @LF : "... duration about 12 seconds." : @LF : @LF : "Avoid any keyboard or mouse activity!"
Display (4, strMsgTitle, strMsgText)

blnResult = ShellExecute ("timedate.cpl", "", "", @NORMAL, "")
TimeDelay (1)
strWinClock = WinGetactive ()
TimeDelay (2)


; Test 1.
fltDelay = 3.4
SendKey ("{SHIFT}") ; Simulate activity.

strMsgText = "Test Step 1" : @LF : @LF : "Wait " : fltDelay : " seconds ..."
Display (fltDelay, strMsgTitle, strMsgText)

arrIdleTime = udfGetIdleTime (1)
intSecondsIdle1 = arrIdleTime[0] ; Rounded to 3 seconds.


; Test 2.
fltDelay = 3.5
SendKey ("{SHIFT}") ; Simulate activity.

strMsgText = "Test Step 2" : @LF : @LF : "Wait " : fltDelay : " seconds ..."
Display (fltDelay, strMsgTitle, strMsgText)

arrIdleTime = udfGetIdleTime (1)
intSecondsIdle2 = arrIdleTime[0] ; Rounded to 4 seconds.


; Test 3.
fltDelay = 4.5
SendKey ("{SHIFT}") ; Simulate activity.

strMsgText = "Test Step 3" : @LF : @LF : "Wait " : fltDelay : " seconds ..."
Display (fltDelay, strMsgTitle, strMsgText)

intSecondsIdle3 = udfGetIdleTime (0) ; Rounded to 5 seconds.


strMsgText = "Result" : @LF : intSecondsIdle1 : @LF : intSecondsIdle2 : @LF : intSecondsIdle3
Display (5, strMsgTitle, strMsgText)

SendKeysTo (strWinClock, "!{F4}")

:CANCEL
Exit