WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

DllCall Information

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Check for Input Idle - No User Input

KEYWORDS:   GetLastInputInfo User Input Idle Mouse Key Keystroke Last Activity Time getTickCount

Note: The following script is fundamentally flawed because the 'GetTickCount' and possiblly the 'GetLastInputInfo 'function will start returning negative numbers after the computer has been up for ~24.85 days and 'GetTickCount' will reset after ~49.7 days. This means that you can get either negative or incorrect positive idle times if the computer is up long enough.
#DefineFunction Idle_Tick()
   ; Get the system uptime
   systemUptime = GetTickCount()

   ; The tick at which the last input was recorded
   LastInputTicks = 0

   ; The number of ticks that passed since last input
   IdleTicks = 0

   ; Set the struct
   LastInputInfo = BinaryAlloc(8)
   BinaryPoke4(LastInputInfo,0,8);cbSize
   BinaryPoke4(LastInputInfo,4,0);dwTime

   ; If we have a value from the function
   ret = DllCall(DirWindows(1):'user32.dll',long:'GetLastInputInfo',lpbinary:LastInputInfo)
   If ret
      ; Get the number of ticks at the point when the last activity was seen
      LastInputTicks = BinaryPeek4(LastInputInfo,4) ;dwTime

      ; Number of idle ticks = system uptime ticks - number of ticks at last input
      IdleTicks = systemUptime - LastInputTicks
   EndIf

   LastInputInfo = BinaryFree(LastInputInfo)

   ;Convert milliseconds to seconds
   SystemUptime = systemUptime / 1000
   IdleTime = IdleTicks / 1000
   LastInput = LastInputTicks / 1000
   Return IdleTime
#EndFunction

;Wait For No user input for 10 seconds
BoxOpen('Check for user input','')
While @TRUE
   tick = Idle_Tick()
   If tick==10
      Pause('Notice','No user input for 10 seconds!')
   EndIf
   BoxText('No user input for ':tick:' seconds')
   TimeDelay(1)
EndWhile

Article ID:   W17403
File Created: 2011:09:01:09:34:10
Last Updated: 2011:09:01:09:34:10