Locking the Keyboard and Mouse at Scheduled Times

Environment: Windows 9x

Introduction

This program locks keyboard and mouse on computers running Windows 9x.

My boss needed something that would prevent users from using the computer at a specified time (e.g., 7 PM). But the user was to be able to use it again next morning (e.g., at 9 AM). The software was intended for use only on Windows 9x systems. After searching for something similar, I decided to write my own.

The result is presented here. At a specified time, it turns off all special keys (Ctrl-Alt-Del, Alt-Tab, Ctrl-Esc, Windows key), then intercepts all keyboard input, as well as WM_LBUTTONDOWN event, so that the user won't be able to do anything on the computer. Rebooting doesn't help either, because this program automatically runs at startup (I know, it might look like a Trojan horse, but that was my requirement :))

To intercept keyboard and mouse input I install a system-wide hook. A hook is a point in the system message-handling mechanism where an application can install a subroutine to monitor the message traffic in the system and process certain types of messages before they reach the target window procedure. To install a hook use SetWindowsHookEx() function. It takes 4 parameters:
    int idHook: Specifies the type of hook procedure to be installed. In this case it is WH_KEYBOARD for keyboard hook and WH_MOUSE for mouse hook.

    HOOKPROC lpfn: Pointer to the hook procedure. Since I want to install a system-wide hook, it must point to a hook procedure in a dynamic-link library (DLL).

    HINSTANCE hMod: Handle to the DLL containing the hook procedure pointed to by the lpfn parameter.

    DWORD dwThreadId: Specifies the identifier of the thread with which the hook procedure is to be associated. If this parameter is zero, the hook procedure is associated with all existing threads running in the same desktop as the calling thread.

To disable special keys (Ctrl-Alt-Del, Alt-Tab, Ctrl-Esc, Windows key) I used SystemParametersInfo() function with SPI_SETSCREENSAVERRUNNING flag. Even though applications are not supposed to use this flag, because it is used internally in Windows 9x, it does the job.

On startup, the program checks if there is another instance of the same program running. If so, it terminates. Otherwise, it checks where it is run from, and tries to install itself in System folder and RunServices key of Windows registry. Afterwards, it renames itself to "mskml32.exe" and tries to delete the original file. Default start time is set to 7 PM, and default end time is 9 AM. These times are specified in "HKLM\Software\Windows\CurrentVersion\Explorer\Services" key. At a specified start time it disables special key combinations, and installs keyboard and mouse hooks. At any time you can type "unl0ck" to remove hooks and enable special key combinations. By the way, in this version password is hard-coded into the program. You are welcome to modify the source code, so that it checks a password from some other location.

Feel free to adapt this program to your special needs. It's a great way to learn how hooks work.

Downloads

Download demo project - 47 Kb


Comments

  • CTRL+ALT+DEL

    Posted by DEX348 on 07/31/2005 01:31pm

    KeyboardControl SDK allows that on all Windows version

    Reply
  • Comments

    Posted by x3r0 on 04/11/2005 01:48am

    I'm surprised you would write any program without any useful comments.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds