PsaltyDS

_Timer_GetIdleTime()

21 posts in this topic

#1 ·  Posted (edited)

This function returns the time (in ticks) since the last user action with mouse/keyboard.

Info on the first DLL call for GetLastInputInfo came from a post by GaryFrost. This method does not require a call to the function for "initializing" before gettting the user idle time, which is why I prefer it to _CheckIdle().

The second call to convert the time is lifted from _Date_Time_GetTickCount().

Seems to work pretty well. Constructive critique is always welcome.

Here's the function:

; #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 $iDiff
    Else
    ; Rollover of ticks counter has occured
        Return SetError(0, 1, $avTicks[0])
    EndIf
EndFunc  ;==>_Timer_GetIdleTime

...and here's the demo script:

; _Timer_GetIdleTime.au3
#include <Timers.au3>

; Mouse/Keyboard action during this 10 sec delay will change reported idle time
Sleep(10 * 1000); 10sec

Global $iIdleTime = _Timer_GetIdleTime()

MsgBox(64, "_Timer_GetIdleTime", "Idle time = " & $iIdleTime & "ms")

:D

Edit: v1.0.1 Fixed bug reported by FitzChivalry

Edit: Reformatted for submission as a UDF.

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

Share this post


Link to post
Share on other sites



This one is a keeper!

Thanks very much :D


Broken link? PM me and I'll send you the file!

Share this post


Link to post
Share on other sites

Very nice! This will be good.


[left][sub]We're trapped in the belly of this horrible machine.[/sub][sup]And the machine is bleeding to death...[/sup][sup][/sup][/left]

Share this post


Link to post
Share on other sites

Reformatted for submission as a possible addition to the Timers.au3 UDF.

:)


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

Share this post


Link to post
Share on other sites

The Timers.au3 UDF includes _Timer_GetIdleTime() as of Beta 3.2.13.3 dated 6/22/08.

:)


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

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

This function returns the time (in ticks) since the last user action with mouse/keyboard.

Info on the first DLL call for GetLastInputInfo came from a post by GaryFrost. This method does not require a call to the function for "initializing" before gettting the user idle time, which is why I prefer it to _CheckIdle().

The second call to convert the time is lifted from _Date_Time_GetTickCount().

Seems to work pretty well. Constructive critique is always welcome.

Here's the function:

; #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 $iDiff
    Else
    ; Rollover of ticks counter has occured
        Return SetError(0, 1, $avTicks[0])
    EndIf
EndFunc  ;==>_Timer_GetIdleTime

...and here's the demo script:

; _Timer_GetIdleTime.au3
#include <Timers.au3>

; Mouse/Keyboard action during this 10 sec delay will change reported idle time
Sleep(10 * 1000); 10sec

Global $iIdleTime = _Timer_GetIdleTime()

MsgBox(64, "_Timer_GetIdleTime", "Idle time = " & $iIdleTime & "ms")

;)

Edit: v1.0.1 Fixed bug reported by FitzChivalry

Edit: Reformatted for submission as a UDF.

How to insert this into a script? Replace the MsgBox with the script?

Edited by jaweb

Share this post


Link to post
Share on other sites

The minimum you need is one line for the #include, and one line to call the function:

#include <Timers.au3>
Global $iIdleTime = _Timer_GetIdleTime()

Now do as you please with the value in $iIdleTime.

You can call the function anywhere in your script. The place to do it depends entirely on what your script is doing.

;)


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

Share this post


Link to post
Share on other sites

I'm making a prank script. ;) Where to insert so it runs when idle.

Can you also explain as if you're talking to a child. :)

#NoTrayIcon
#include <IE.au3>
while 1
$oIE = _IECreate()
$hIE = _IEPropertyGet($oIE, "hwnd") 
WinSetState($hIE, "", @SW_MAXIMIZE) 
    _IENavigate($oIE, "https://secure.wikimedia.org/wikipedia/en/wiki/Artificial_intelligence")
    _IELoadWait ($oIE)
MouseClick("left",Random(100,500,1),Random(100,500,1))
    Sleep(2000)
    _IELoadWait ($oIE)
MouseClick("left",Random(100,500,1),Random(100,500,1))
    Sleep(2000)
    _IELoadWait ($oIE)
MouseClick("left",Random(100,500,1),Random(100,500,1))
    Sleep(2000)
    _IELoadWait ($oIE)
    $PID = ProcessExists("IEXPLORE.EXE") ; Will return the PID or 0 if the process isn't found.
If $PID Then ProcessClose($PID)
_IEQuit ($oIE)
Sleep(Random(60000,70000))
WEnd

Share this post


Link to post
Share on other sites

I'm making a prank script...

Can you also explain as if you're talking to a child.

It seems I am.

;)


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

Share this post


Link to post
Share on other sites

Dear professionals, tell me, please, why _Timer_GetIdleTime() function does not work when the wireless headset? Can I fix it somehow?

Share this post


Link to post
Share on other sites

It's a headset, it's not a mouse or keyboard, does it wake your computer from the screensaver if you use it?


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

It is interesting that the screen saver continues to work with the headset, but the function ceases. And the messenger program also comes from "away." :huh:

If it helps somehow, the model is a wireless headset: Rapoo H8020.

Another issue here is associated with it:

Perhaps there is a connection in this?!

Edited by AndreyS

Share this post


Link to post
Share on other sites

I'm not sure what your problem is with the function and your headset. What is it you're trying to accomplish with this that isn't working correctly with your headset?


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

Explain! I have a program designed to Autoit that depend on the function _Timer_GetIdleTime (). But if the headset works, the function always returns about 16ms. If you turn off the headset is idle time is right.

Thus, when the function is working headset _Timer_GetIdleTime () does not consider the time of computer inactivity. On the headset because I do not press any buttons at the same time! But the screensaver works with the headset on!

How can I fix this in the _Timer_GetIdleTime () to make it work with the headset?

I would be very grateful!

Edited by AndreyS

Share this post


Link to post
Share on other sites

So can anyone tell me at least how to change the function _Timer_GetIdleTime() (Timers.au3) that it does not respond to mouse movements? :mellow:

Share this post


Link to post
Share on other sites

You can't, it monitors activity, and using the mouse is activity. You'd need to do it another way to have it not monitor the mouse.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Yes, yes! But how? Can tell me? I think if it is possible to track only pressing a button keyboard and mouse, it will work with my headset.

Share this post


Link to post
Share on other sites

I can only think of using _IsPressed, or something similar, to detect when a key is pressed either on the keyboard or the mouse, and resetting a timer if it detects one or the other.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

I just started thinking about this but so far no ideas.

Share this post


Link to post
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