Recording PC idle time

This script is written in AutoIT and checks for when the PC has been idle (no keyboard presses or mouse movements) for a specific length of time. Currently the script logs to screen but you should be able to easily get it to log to a file and database, although the latter will require more work from you to get it working.

Here’s the script:

#AutoIt3Wrapper_Au3Check_Parameters= -d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6

#include <Timers.au3>

$data = InputBox("Threshold", "Record user input if the PC has been idle for (in seconds)")

; Mouse/Keyboard action during this 10 sec delay will change reported idle time
Global $SleepDuration = 10 * 1000 ; 10sec
Global $TotalIdleTime = 0
Global $Count = 0
Global $Threshold = $data*1000

; Create your database manually then add these
Global $MyDB="UserIdle"
Global $MyUser="Testuser"
Global $MyPwd="TestPass"
Global $MySvr="MyDBServer"

while 0 = 0
    Global $IdleTime = _Timer_GetIdleTime()

    ConsoleWrite("DEBUG: idle = " & $IdleTime & @LF)

    if $IdleTime > $SleepDuration-2 Then
        $TotalIdleTime = $IdleTime
        if $TotalIdleTime > $Threshold then
            ConsoleWrite ("Machine returned from idle after " & int(($TotalIdleTime + ($SleepDuration - $IdleTime))/1000) & " seconds" & @LF)
        $TotalIdleTime = $IdleTime

func write_to_db()
    $conn = ObjCreate( "ADODB.Connection" )  
    $rs = ObjCreate( "ADODB.RecordSet" )  
    ;You'll need to change this insert to match the name of the table and the columns in your database
    $rs.Open( "INSERT some data into my database", $conn )  

On running the script prompts with a dialogue box for the number of minutes to wait before treating the PC as idle and logging the event.


Grab and install AutoIT from their website. Use the AutoIT editor to tweak the script how you’d like. To compile open “AutoIT Script to exe converter” from the AutoIT start menu group. Choose the options as you see fit and compile. There are compilers for x64 and x32 which you’ll find in the <program files>\AutoIt3\Aut2Exe directory.

Heres the source and binaries to download:

Rename from doc to zip

Tagged , ,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: