To use this class you will need to download the basic module at http://www.codeoftheweek.com/issues/issue102
Create a new class module and paste this source code into it. You should name this class module cInactivityWatch. If you have any questions, email us at help@codeoftheweek.com
'----------------------------------------------------------------------
'
' Module Name: cInactivityWatch
' Written By: C&D Programming Corp.
' Create Date: 8/99
' Copyright: Copyright 1999 by C&D Programming Corp. Source
' code may not be reproduced except for use in a
' compiled executable. All rights reserved. If
' you would like to reprint any or all of this
' code please email us at info@codeoftheweek.com
'----------------------------------------------------------------------
Option Explicit
Public InactivityDelay As Long ' in seconds
Public InactivityInterval As Long ' how often to check for inactivity in seconds (we
' recommend this is 30 seconds or less)
Public Event Inactivity() ' raised when inactive for the specified amount of time.
Private mbHookActive As Boolean
' true when the inactivity watch is activated
Public Property Get Active() As Boolean
Active = mbHookActive
End Property
' this is the routine that will raise the inactivity event
Public Sub UserInactivity()
RaiseEvent Inactivity
End Sub
' call this when you want to begin checking for inactivity
Public Sub Activate()
Call basInactivity.SetParameters(Me, InactivityDelay, InactivityInterval)
mbHookActive = SetInactivityHook
End Sub
' call this when you want to stop checking for inactivity
Public Sub Inactivate()
ResetInactivityHook
mbHookActive = False
End Sub
Private Sub Class_Initialize()
InactivityDelay = 30 ' 30 seconds
InactivityInterval = 1
mbHookActive = False
End Sub
Public Property Get LastKeyboardActivityTime() As Date
LastKeyboardActivityTime = ActivityRef(imKeyboard)
End Property
Public Property Get LastMouseActivityTime() As Date
LastMouseActivityTime = ActivityRef(imMouse)
End Property
Public Property Get LastKeyboardActivitySeconds() As Long
LastKeyboardActivitySeconds = Abs(DateDiff("s", Now, ActivityRef(imKeyboard)))
End Property
Public Property Get LastMouseActivitySeconds() As Long
LastMouseActivitySeconds = Abs(DateDiff("s", Now, ActivityRef(imMouse)))
End Property
Public Property Get LastActivitySeconds() As Long
Dim lMouse As Long
Dim lKeyboard As Long
lMouse = Abs(DateDiff("s", Now, ActivityRef(imMouse)))
lKeyboard = Abs(DateDiff("s", Now, ActivityRef(imKeyboard)))
LastActivitySeconds = IIf(lMouse > lKeyboard, lKeyboard, lMouse)
End Property
Public Property Get LastActivityTime() As Date
Dim dMouse As Date
Dim dKeyboard As Date
dMouse = ActivityRef(imMouse)
dKeyboard = ActivityRef(imKeyboard)
LastActivityTime = IIf(dMouse < dKeyboard, dKeyboard, dMouse)
End Property
Private Sub Class_Terminate()
' make sure we clean up if the hook is active
If Active Then
Inactivate
End If
End Sub