Click here to Skip to main content
11,924,995 members (56,072 online)
Click here to Skip to main content
Add your own
alternative version


60 bookmarked

Disable keyboard and show images for the children

, 21 Feb 2005
Rate this:
Please Sign up or sign in to vote.
An application to let run every time a child is over your computer.

The application showing a cool image.


I wrote this article, not only as a computer engineer I am, but also as a father I became of two marvelous children I have. I have always turned on the computer and my children try to use it by clicking the mouse, and striking, smashing and smiting the keyboard. Often, they push the 'Windows' key or the 'Esc' key, or some other key that finishes the execution of some programs, starts applications, etc… That is the reason why I developed this little tool that allows my 'marvelous beasts' to hit the keys they want and the only thing that occurs is that an image will be changed.

What this application does is, cancels all the keyboard events except:

  • Ctrl+Alt+Del Captured by OS and
  • Ctrl+Alt+End used for finishing the application.

All the other events will be captured and ignored. On the other hand, it will show an image randomly taken from the images stored in the directory Images, and this image will change every certain number of seconds defined in the application. Also, it will change the mouse cursor with an icon randomly taken from the directory Images every certain number of seconds or every 100 mouse move events.

Before executing the demo

Before executing the demo, we must get some BMPs and ICOs and put them into the Images directory.

Using the code

The application is very simple, and here I only present a minimal version just for maintaining the code clean and easy to read. There are three classes:

  • CForTheKidsApp: the application class, created by the environment.
  • CForTheKidsDlg: the dialog class that will be shown and having the control-view.
  • CImageLoader: an auxiliary class that will help us find what images to load and load it into a static control, cursor, etc…

The main function that is called is in the initialization of the dialog (CForTheKidsDlg::OnInitDialog()) and it will do the Hook to the keyboard. This hook procedure will monitor low-level keyboard input events. As the procedure that will be called is a global procedure, and we will need the hook itself, we declare a global variable called TheKeyHook that will have the HHOOK handle. For doing that, we execute the following statement:

TheKeyHook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, ppI,0);

We want that the LowLevelKeyboardProc will filter all the keyboard events except one combination that will let us finish the application. We decide that this combination could be Ctrl+Alt+End because it is a combination that a child rarely can execute because of the separation that exists between those keys. This procedure must return 1 if we want that the event be ignored.

That is the reason that if the code is a HC_ACTION then we always return 1, except for the Ctrl+Alt+End combination in which case we exit the application. For more information about this, see LowLevelKeyboardProc in MSDN.

It is important to remark that before we exit the application, we must unhook the keyboard event and send a message to the main window to close, using the following sentences:


The LowLevelKeyboardProc procedure will be the following:

LRESULT CALLBACK LowLevelKeyboardProc(INT nCode, WPARAM wParam, LPARAM lParam)
        // By returning a non-zero value from the hook procedure, the
        // message does not get passed to the target window
        int error=GetLastError();   
        switch (nCode)
            case HC_ACTION:
                // Disable all keys except CTRL + ALT + END. 
                // This combination will finish the aplication
                BOOL bControlKeyDown = 
                  GetAsyncKeyState (VK_CONTROL) >> ((sizeof(SHORT) * 8) - 1);
                if (pkbhs->vkCode == VK_END && 
                    LLKHF_ALTDOWN && bControlKeyDown)
                    return 1;

        CallNextHookEx (TheKeyHook, nCode, wParam, lParam);
    return 0;

Another important thing that it is not explained directly in most sites is that if you want to use the WH_KEYBOARD_LL flag, you must have the _WIN32_WINNT equal or greater than 1024 (0x0400). For doing that, we must include into the settings the following Preprocessor definition:


Special points to consider

  • The images are changed using a timer, initially set with the defines _TIMER_ELAPSE_BMP_ and _TIMER_ELAPSE_ICO_.
  • The timers have the identifiers defined with _TIMER_CHANGEBMP_ and _TIMER_CHANGEICO_.
  • The images are taken from the directory Images placed where the application is executed.

Ideas for improving the application

  • The code, as is, is just the beginning of what this program can do. I only change the image every N milliseconds, but we can add an OnLButtonDown event, an OnRButtonDown event, and we can use the hook for intercepting some keys that we would want to have some special behavior.
  • It only reads .bmp and .ico files because it is not the aim of this tool to read all kinds of images, and adding this functionality will only complicate the reading of the code. If you want, you can easily add other kinds of images into the CImageLoader class.
  • The image directory, elapsed time, etc… are hard-coded, but it can be easily extracted and put into an .ini file.
  • If we want, we can use this tool to teach numbers and letters to our children. For instance, we show an image of the letter P and we want that the child press that key. If he or she does that, we can show a beautiful image or play a nice music. They can investigate all the keys, and those keys we do not want them to touch, we can easily 'cancel' from the system.
  • In my computer, I download lots of images of bulldozers and trains, and every time my child smashes the keyboard, another train/bulldozer appears and he has fun!

Remarks for debugging

  • If we debug the application, we must not use the hook, because the keyboard will be ignored, and if a breakpoint is set, the application will stop and the keyboard 'hangs'. In this case, we must finish the IDE, losing all the data not saved and, often, we must restart the computer.
  • The main dialog CForTheKidsDlg, has the System modal property enabled, which prohibits switching to another window or program while the dialog box is active. Take care while debugging to set it to false allowing the debugger to be switched.


Minimal version.

Version 1.0 with memory leak solved, thanks to VaKa.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Spain Spain
Software engineer specialist in OO analisys and design.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
mk1488217-Sep-10 1:14
membermk1488217-Sep-10 1:14 
QuestionDisable Keyboard Question... Pin
gn0058895012-Mar-10 17:59
membergn0058895012-Mar-10 17:59 
AnswerRe: Disable Keyboard Question... Pin
pocjoc15-Mar-10 3:09
memberpocjoc15-Mar-10 3:09 
GeneralRe: Disable Keyboard Question... Pin
gn0058895015-Mar-10 18:55
membergn0058895015-Mar-10 18:55 
GeneralRe: Disable Keyboard Question... Pin
pocjoc18-Mar-10 6:25
memberpocjoc18-Mar-10 6:25 
Generalkeyboard input to two process Pin
hgm6887-Jun-08 11:16
memberhgm6887-Jun-08 11:16 
GeneralRe: keyboard input to two process Pin
pocjoc10-Jul-08 3:55
memberpocjoc10-Jul-08 3:55 
Questiondetect external keyboard? Pin
ahmetsacan23-Feb-08 8:54
memberahmetsacan23-Feb-08 8:54 
AnswerRe: detect external keyboard? Pin
pocjoc24-Feb-08 22:10
memberpocjoc24-Feb-08 22:10 
Questionundeclared identifier error..... Pin
ponnalagan22-Jun-07 1:45
memberponnalagan22-Jun-07 1:45 
AnswerRe: undeclared identifier error..... Pin
pocjoc22-Jun-07 2:01
memberpocjoc22-Jun-07 2:01 
AnswerRe: undeclared identifier error..... Pin
k_selvanathan22-Jun-07 2:14
memberk_selvanathan22-Jun-07 2:14 
GeneralRe: undeclared identifier error..... Pin
ponnalagan22-Jun-07 2:23
memberponnalagan22-Jun-07 2:23 
Generalprojects Pin
vaibhva21-Jun-07 19:59
membervaibhva21-Jun-07 19:59 
Generalf Pin
vaibhva21-Jun-07 19:50
membervaibhva21-Jun-07 19:50 
QuestionPerfect!!! Pin
JTrujillo_ISI14-Mar-07 7:35
memberJTrujillo_ISI14-Mar-07 7:35 
AnswerRe: Perfect!!! Pin
pocjoc14-Mar-07 22:36
memberpocjoc14-Mar-07 22:36 
GeneralWindow movement restriction Pin
AKG20-Feb-07 23:06
memberAKG20-Feb-07 23:06 
GeneralRe: Window movement restriction Pin
pocjoc23-Feb-07 0:45
memberpocjoc23-Feb-07 0:45 
QuestionHow GetWindowThreadProcessId() works? Pin
Esonix14-Feb-07 0:12
memberEsonix14-Feb-07 0:12 
AnswerRe: How GetWindowThreadProcessId() works? Pin
pocjoc14-Feb-07 1:05
memberpocjoc14-Feb-07 1:05 
QuestionHi - Help! [modified] Pin
mv_xm8-Nov-06 8:58
membermv_xm8-Nov-06 8:58 
AnswerRe: Hi - Help! Pin
pocjoc9-Nov-06 1:31
memberpocjoc9-Nov-06 1:31 
GeneralRe: Hi - Help! Pin
mv_xm9-Nov-06 4:19
membermv_xm9-Nov-06 4:19 
QuestionRe: Hi - Help! Pin
mv_xm9-Nov-06 4:53
membermv_xm9-Nov-06 4:53 
AnswerRe: Hi - Help! Pin
pocjoc10-Nov-06 1:30
memberpocjoc10-Nov-06 1:30 
QuestionRe: Hi - Help! Pin
mv_xm10-Nov-06 2:41
membermv_xm10-Nov-06 2:41 
AnswerRe: Hi - Help! Pin
pocjoc10-Nov-06 2:53
memberpocjoc10-Nov-06 2:53 
JokeRe: Hi - Help! Pin
mv_xm10-Nov-06 5:53
membermv_xm10-Nov-06 5:53 
Generalsimpler Pin
jqt12-Sep-06 14:25
memberjqt12-Sep-06 14:25 
AnswerRe: simpler Pin
pocjoc12-Sep-06 21:59
memberpocjoc12-Sep-06 21:59 
QuestionHow to disable CTRL+ALT+DEL ? Pin
Cold-Fire11-Aug-06 4:27
memberCold-Fire11-Aug-06 4:27 
AnswerRe: How to disable CTRL+ALT+DEL ? Pin
pocjoc9-Nov-06 1:39
memberpocjoc9-Nov-06 1:39 
GeneralRe: How to disable CTRL+ALT+DEL ? Pin
Jerry Jeremiah14-Oct-07 19:39
memberJerry Jeremiah14-Oct-07 19:39 
QuestionHow to make it work under windows 98 and lower? Pin
ly2157672125-Apr-06 0:38
memberly2157672125-Apr-06 0:38 
AnswerRe: How to make it work under windows 98 and lower? Pin
pocjoc25-Apr-06 21:53
memberpocjoc25-Apr-06 21:53 
GeneralRe: How to make it work under windows 98 and lower? Pin
ly2157672126-Apr-06 4:06
memberly2157672126-Apr-06 4:06 
easter_20077-Feb-06 12:11
membereaster_20077-Feb-06 12:11 
pocjoc8-Feb-06 0:39
memberpocjoc8-Feb-06 0:39 
easter_200720-Feb-06 22:57
membereaster_200720-Feb-06 22:57 
QuestionRe: WH_KEYBOARD_LL Pin
pocjoc20-Feb-06 23:07
memberpocjoc20-Feb-06 23:07 
GeneralUndeclared identifier KBDLLHOOKSTRUCT Pin
mohsen nourian26-Dec-05 5:09
membermohsen nourian26-Dec-05 5:09 
GeneralRe: Undeclared identifier KBDLLHOOKSTRUCT Pin
aTxIvG400220-Dec-06 4:03
memberaTxIvG400220-Dec-06 4:03 
GeneralRe: Undeclared identifier KBDLLHOOKSTRUCT Pin
gonadthebarbarian8-Jan-07 7:24
membergonadthebarbarian8-Jan-07 7:24 
GeneralRe: Undeclared identifier KBDLLHOOKSTRUCT Pin
jst2tzu5-Mar-07 13:15
memberjst2tzu5-Mar-07 13:15 
QuestionHow to stop other messages from happening? Pin
luyongxing1-Dec-05 14:27
memberluyongxing1-Dec-05 14:27 
AnswerRe: How to stop other messages from happening? Pin
gonadthebarbarian8-Jan-07 8:04
membergonadthebarbarian8-Jan-07 8:04 
GeneralMemory Leak Pin
VaKa15-Feb-05 21:12
memberVaKa15-Feb-05 21:12 
GeneralRe: Memory Leak Pin
pocjoc16-Feb-05 1:52
memberpocjoc16-Feb-05 1:52 
GeneralRe: Memory Leak Pin
James R. Twine16-Feb-05 4:05
memberJames R. Twine16-Feb-05 4:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151125.3 | Last Updated 21 Feb 2005
Article Copyright 2005 by pocjoc
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid