Disable task switching keys with c++

孤街醉人 提交于 2019-12-23 03:46:15

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/24134998/disable-task-switching-keys-with-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!