2

I've done a lot of searching around with no real solution (to my own problem) so I thought I'd ask here.

I'm designing a kiosk-like program that prevents the user from using task keys (alt+tab, alt+esc, ctrl+esc, etc) while the program is running. Note I'm a novice programmer thus I'd want to stay away from separate dll handling if I can. Particularly, I have went to this site http://support.microsoft.com/kb/226359/en-us for the code. A simplified part of my code looks like this at the top:

HHOOK mule;
HHOOK g_hKeyboardHook;
BOOL g_bFullscreen;
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
    KBDLLHOOKSTRUCT *pkbhs = (KBDLLHOOKSTRUCT *) lParam;
    BOOL bControlKeyDown = 0;

    switch (nCode)
    {
        case HC_ACTION:
        {
            // Check to see if the CTRL key is pressed
            bControlKeyDown = GetAsyncKeyState (VK_CONTROL) >> ((sizeof(SHORT) * 8) - 1);

            // Disable CTRL+ESC
            if (pkbhs->vkCode == VK_ESCAPE && bControlKeyDown)
                return 1;

            // Disable ALT+TAB
            if (pkbhs->vkCode == VK_TAB && pkbhs->flags & LLKHF_ALTDOWN)
                return 1;

            // Disable ALT+ESC
            if (pkbhs->vkCode == VK_ESCAPE && pkbhs->flags & LLKHF_ALTDOWN)
                return 1;

            break;
        }

        default:
            break;
    }
    return CallNextHookEx (mule, nCode, wParam, lParam);
}

The my main is

int main(int argc, char **argv)
{
    _getch();
    g_hKeyboardHook = SetWindowsHookEx( WH_KEYBOARD_LL,  LowLevelKeyboardProc, GetModuleHandle(NULL), 0 );
    cout << "Testing task keys disabling (alt tab, ctrl esc, alt esc) and taskbar..." << endl;
    _getch();
     UnhookWindowsHookEx( g_hKeyboardHook );
    cout << "Re enabled" << endl;
    _getch();
return 0;
}

I realize this code is for really old windows OS, but I've looked around and the other solutions resemble this code so I thought it should work.

But for some reason it doesn't seem to be working. Whenever my program gets to that line of code, the program stalls for like 5 seconds and continues to run, but the task keys are still working.

I've heard that I should be implementing that function as a dll instead of putting everything in one file, but I'm not sure if they're absolutely right (also I know nothing of dlls)

In addition, I've also tried code (to disable windows key) here: http://msdn.microsoft.com/en-us/library/windows/desktop/ee416808(v=vs.85).aspx and it does the same thing my own program (stalls and does nothing)

Can anyone spot where I did something wrong? I'm using VC++ 2010 on windows 7 64bit.

2
  • For a real kiosk, you'd need something truly robust. In particular, you need to deal with Ctrl-Alt-Del. That will be hard for a novice programmer, as that sequence is usually handled by Windows directly.
    – MSalters
    Jun 10, 2014 at 8:11
  • I have handled Ctrl-alt-del already by disabling the task manager through registry keys (which my superior said was enough for this task), but my real problem here is disabling the task keys
    – Sophia
    Jun 10, 2014 at 8:27

1 Answer 1

0

Your code is fine, hooks just doesn't work with console application because windows can't callback into a console application, it requires a message loop.

Read this answer by Hans Passant which applies here too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.