Skip to main content
Email Password   helpLost your password?

The application showing a cool image.

Introduction

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:

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:

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:

UnhookWindowsHookEx(TheKeyHook);
SendMessage(TheMainWndHnd,WM_CLOSE,0,0);

The LowLevelKeyboardProc procedure will be the following:

LRESULT CALLBACK LowLevelKeyboardProc(INT nCode, WPARAM wParam, LPARAM lParam)
{
    try
    {
        // By returning a non-zero value from the hook procedure, the

        // message does not get passed to the target window

        KBDLLHOOKSTRUCT *pkbhs = (KBDLLHOOKSTRUCT *) lParam;
        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)
                {
                    UnhookWindowsHookEx(TheKeyHook);
                    SendMessage(TheMainWndHnd,WM_CLOSE,0,0);
                }
                else
                    return 1;
                break;
            } 

            default:
                break;
        }
        CallNextHookEx (TheKeyHook, nCode, wParam, lParam);
    }
    catch(...)
    {
    }
    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:

_WIN32_WINNT=1024

Special points to consider

Ideas for improving the application

Remarks for debugging

History

Minimal version.

Version 1.0 with memory leak solved, thanks to VaKa.

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
Generaljust what i need! Pin
maretzel
20:04 6 Aug '08  
Generalkeyboard input to two process Pin
hgm688
11:16 7 Jun '08  
GeneralRe: keyboard input to two process Pin
pocjoc
3:55 10 Jul '08  
Generaldetect external keyboard? Pin
ahmetsacan
8:54 23 Feb '08  
GeneralRe: detect external keyboard? Pin
pocjoc
22:10 24 Feb '08  
Questionundeclared identifier error..... Pin
ponnalagan
1:45 22 Jun '07  
AnswerRe: undeclared identifier error..... Pin
pocjoc
2:01 22 Jun '07  
AnswerRe: undeclared identifier error..... Pin
k_selvanathan
2:14 22 Jun '07  
GeneralRe: undeclared identifier error..... Pin
ponnalagan
2:23 22 Jun '07  
Generalprojects Pin
vaibhva
19:59 21 Jun '07  
Generalf Pin
vaibhva
19:50 21 Jun '07  
QuestionPerfect!!! Pin
JTrujillo_ISI
7:35 14 Mar '07  
AnswerRe: Perfect!!! Pin
pocjoc
22:36 14 Mar '07  
GeneralWindow movement restriction Pin
AKG
23:06 20 Feb '07  
GeneralRe: Window movement restriction Pin
pocjoc
0:45 23 Feb '07  
GeneralHow GetWindowThreadProcessId() works? Pin
Esonix
0:12 14 Feb '07  
GeneralRe: How GetWindowThreadProcessId() works? Pin
pocjoc
1:05 14 Feb '07  
QuestionHi - Help! [modified] Pin
mv_xm
8:58 8 Nov '06  
AnswerRe: Hi - Help! Pin
pocjoc
1:31 9 Nov '06  
GeneralRe: Hi - Help! Pin
mv_xm
4:19 9 Nov '06  
QuestionRe: Hi - Help! Pin
mv_xm
4:53 9 Nov '06  
AnswerRe: Hi - Help! Pin
pocjoc
1:30 10 Nov '06  
QuestionRe: Hi - Help! Pin
mv_xm
2:41 10 Nov '06  
AnswerRe: Hi - Help! Pin
pocjoc
2:53 10 Nov '06  
JokeRe: Hi - Help! Pin
mv_xm
5:53 10 Nov '06  


Last Updated 21 Feb 2005 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009