Visual Basic Code of the Week (COTW)
Issue #102
Online Version at (paid subscribers only)
All content and source code is Copyright (c) 1999 by C&D Programming Corp. No part of this issue can be reprinted or distributed in any manner without express written permission of C&D Programming Corp.

This week's topic: Checking for keyboard/mouse inactivity - Part 1

Six months of VB Training for only $49.99

Want to get up to speed on the latest Visual Basic programming? Includes Visual Basic 6 and Visual InterDev 6. Check out our training programs at

Get paid to surf the web!

If you would like to get paid for surfing the web, jump to


In this Issue

This issue introduces a module and class combination which can monitor keyboard and mouse activity. It provides the last time the mouse or keyboard was moved or touched. Since you only need to understand how the class module works to use this source code that is all we will document in this issue (in order to keep the issue a reasonable length). Next week we will jump into the module and explain the API details.

The only limitation we are aware of with this class is that it can only monitor the keyboard and mouse activity within your application.

A good use of this routine is to log a user out of an application after a specified period of inactivity.

If you have any questions about using this module, let us know at


Public InactivityDelay As Long

Number of seconds before an event will be raised to notify the programmer that inactivity has occurred.

Public InactivityInterval As Long

How often to check for inactivity in seconds. If the InactivityDelay is several minutes or longer we recommend setting this parameter to 5 or more seconds. A timer is used to periodically check to see if the inactivity event should be raised.

Public Event Inactivity()

Raised when application is inactive for the specified amount of time.

Public Property Get Active() As Boolean

True if this class is currently monitoring the activity of the keyboard and mouse.

Public Property Get LastKeyboardActivityTime() As Date

Last time keyboard activity has occurred.

Public Property Get LastMouseActivityTime() As Date

Last time mouse activity has occurred.

Public Property Get LastKeyboardActivitySeconds() As Long

Number of seconds since the last keyboard activity has occurred.

Public Property Get LastMouseActivitySeconds() As Long

Number of seconds since the last mouse activity has occurred.

Public Property Get LastActivitySeconds() As Long

Number of seconds since the last activity (either keyboard or mouse) has occurred.

Public Property Get LastActivityTime() As Date

Time since the last activity (either keyboard or mouse) has occurred.


Public Sub UserInactivity()

This routine is called when the application has been inactivated for InactivityDelay seconds. This routine simply raises an event.

Public Sub Activate()

This should be called to initiate the inactivity monitoring process.

Public Sub Inactivate()

This routine should be called to terminate the inactivity monitoring process.


See above descriptions.

Sample Usage

The sample included at shows how to use the above class. It provides a form that shows keyboard and mouse inactivity timers.

  ' See zip file at for a detailed sample

Source Code

To see the source code for this issue you must be a subscriber to Code of the Week. If you are a subscriber the source code is available at the following address:

This document is available on the web

Paid subscribers can view this issue in HTML format. There is no additional source or information in the HTML formatted document. It just looks a little better since we have included some HTML formatting. Just point your browser to link at the top of this document.

Other links

Contact Information

C&D Programming Corp.
PO Box 20128
Floral Park, NY 11002-0128
Phone or Fax: (212) 504-7945