Visual Basic System Services
GetLastInputInfo: Determining System Idle Time
     
Posted:   Friday December 09, 2005
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6
Developed with:   VB6, Windows XP
OS restrictions:   Windows 2000 or later
Author:   VBnet - Randy Birch
     

Related:  

ExitWindowsEx: Shut Down, Reboot, Log Off or Power Off
     
 Prerequisites
Windows 2000 or later.

GetLastInputInfo retrieves the time of the last system input event (mouse or keyboard action) into a LASTINPUTINFO structure. The key member of this type is the dwTime member, which holds the tick count of the last input. This API is typically called as needed to determine how long since the last keyboard or mouse input.

In this demo the code is in a Timer event in order to show how the time changes as interaction with Windows is made. The value returned by in dwTime (when the last event occurred) is subtracted from the the value returned by GetTickCount (the current tick count of the system). The difference is the number of milliseconds that the system has been idle. Note that the Timer code uses FormatNumber, so pre-VB6 devs need to use a different Format function.

After completing the form below, hit run and then don't move the mouse. The label will update as the idle time accumulated; clicking or moving the mouse, or pressing any key will cause the idle time to reset. The command button can be used to suspend/reactive the timer.

 BAS Module Code
None.

 Form Code
To a form, add a label (Label1) and a command button (Command1) along with the following code:

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright 1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Type LASTINPUTINFO
   cbSize As Long
   dwTime As Long
End Type

Private Declare Function GetTickCount Lib "kernel32" () As Long

Private Declare Function GetLastInputInfo Lib "user32" (plii As Any) As Long


Private Sub Form_Load()

   Command1.Caption = "Stop"
   Timer1.Interval = 10  'ms
   Timer1.Enabled = True
   Label1.Caption = "system idle (secs):"
   
End Sub


Private Sub Command1_Click()

   Timer1.Enabled = Not Timer1.Enabled
   
   Select Case Timer1.Enabled
      Case True
         Command1.Caption = "Stop"
      Case False
         Command1.Caption = "Start"
   End Select
      
End Sub


Private Sub Form_Unload(Cancel As Integer)

   Timer1.Enabled = False
   
End Sub


Private Sub Timer1_Timer()

   Dim lii As LASTINPUTINFO

   lii.cbSize = Len(lii)
   Call GetLastInputInfo(lii)
   
   With Label1
      .Caption = "system idle (secs): " & FormatNumber((GetTickCount() - lii.dwTime) / 1000, 2)
      .Refresh
   End With
   
End Sub
 Comments
 

 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright 1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

Hit Counter