VB and VBA Users Source Code: Determining the amount of time the system has been idle
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Determining the amount of time the system has been idle
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Friday, July 29, 2005
Hits:
2639
Category:
Windows API
Article:
The function below can be used to return the amount of time (ms) the system (or computer) has been idle (ie, has not received any mouse or keyboard input). This can be useful for closing database connections or other such clean up tasks. Option Explicit Private Type PLASTINPUTINFO cbSize As Long dwTime As Long End Type Private Declare Function GetLastInputInfo Lib "user32.dll" (ByRef plii As PLASTINPUTINFO) As Long Private Declare Function GetTickCount Lib "kernel32" () As Long 'Purpose : Returns the number of milliseconds the system (or computer) has been idle for. 'Inputs : N/A 'Outputs : Returns the number of milliseconds the system has been idle for. 'Date : 25/03/2005 'Author : Andrew Baker(www.vbusers.com) 'Notes : Public Function SystemIdleTime() As Long Dim tLastInput As PLASTINPUTINFO tLastInput.cbSize = Len(tLastInput) Call GetLastInputInfo(tLastInput) SystemIdleTime = GetTickCount - tLastInput.dwTime End Function 'Test (for VBA) Sub Test() Application.OnTime DateAdd("s", 5, Now), "CheckIdle" End Sub Sub CheckIdle() Debug.Print "The system has been idle for " & SystemIdleTime & " ms" End Sub
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder