If you would like to get paid for surfing the web, jump to http://www.codeoftheweek.com/paidsurf.html
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 http://www.codeoftheweek.com/vbtraining.html
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 questions@codeoftheweek.com
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:
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 callback@codeoftheweek.com
The sample included at http://www.codeoftheweek.com/issues/issue102 shows how to use the above class. It provides a form that shows keyboard and mouse inactivity timers.
' See zip file at http://www.codeoftheweek.com/issues/issue102 for a detailed sample.
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: http://www.codeoftheweek.com/membersonly/bi/0103.html
If you would like to get paid for surfing the web, jump to http://www.codeoftheweek.com/paidsurf.html