Visual Basic Code of the Week (COTW)
Issue #103
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.

Issue topic: Checking for keyboard/mouse inactivity - Part 2

Get paid to surf the web!

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

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


In this Issue

This issue expands on last weeks issue to further explain how the API calls perform their magic to monitor keyboard and mouse activity.

The only limitation we are aware of with this approach is that it can only monitor the keyboard and mouse activity within your application, not across all Windows applications.

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 Enum InactivityMode

This enumerator is used as a parameter to ActivityRef. Currently eMode can only be one of two values: imKeyboard or imMouse. imKeyboard will specify keyboard activity and imMouse will specify mouse activity.


Public Function ActivityRef(eMode As InactivityMode) As Date

Retrieves the last date/time activity was found for the device specified by eMode. Currently eMode can only be one of two values: imKeyboard or imMouse.

Public Function SetInactivityHook() As Boolean

Creates the hooks to monitor the keyboard and mouse activity. Returns True if successful and False if unsuccessful.

This routine calls several API calls, most importantly SetWindowsHookEx which allows your application to hook into the keyboard and mouse activity chain for monitoring. If you really wanted to you, could actually perform some processing of the activity (keystrokes would probably be more likely) other than just watching for those events to occur. When using this API call the keyword AddressOf is used to return the address of your callback routine for passing into SetWindowsHookEx. The callback routine must be in a standard module; it can not be in a form or a class module. For those of you unfamiliar with callbacks see the section below describing them.

It is critical that you release the hooks before your application ends (using ResetInactivityHook) or you will create some spectacular system crashes.


Public Sub SetParameters(o As cInactivtyWatch, lInactivityDelay As Long, lInactivityInterval As Long)

This provides the module with a way to know where to raise the event when inactivity occurs. Refer to last weeks issue for details on the cInactivityWatch class. This routine also saves the Delay amount and Interval amounts for later use (refer to last weeks issue for more details).

Public Sub ResetInactivityHook()

Releases the monitoring of the keyboard and mouse, which is the reverse of the process started with SetInactivityHook.

The API call UnhookWindowsHookEx is used to accomplish this task. It uses the handles received in SetInactivityHook to cancel the monitoring of the keyboard and mouse.


See above descriptions.


Some other interesting notes about this module:

About callbacks

For those of you that have not used callbacks before here is a brief description. When your application needs to watch for some event to occur there are primarily two ways to accomplish this.

The first is to create a loop in your code which repeatedly checks for some condition. This usually is not a desirable way to accomplish this tasks since it will eat up all the available CPU time while processing the code in your loop.

The second is to provide the address of a routine called a CALLBACK to the routine which will can determine the event you are watching for in an efficient manner (In our case it was the keyboard/mouse hook). When that routine gets the event it will make a call to your callback routine to let your code know that the event has occured. This is a very desirable way to perform this task because your code can continue on its way and it will be notified if and when the event you are watching for occurs.

The simplest form of a callback is probably the timer control in Visual Basic. When you put the timer control on your form the Timer event fires when appropriate. This is in essence a callback.

If anyone has a better description of callbacks, pass it along and we will pass it along to our readers. Send an email to

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.

Get paid to surf the web!

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

Other links

Contact Information

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