在Dialog程序中使用WH_KEYBOARD_LL类型HOOK

在Dialog程序中使用WH_KEYBOARD_LL类型HOOK的方法:

SetWindowsHookEx(WH_KEYBOARD_LL, (HOOKPROC)LowLevelKeyboardProc, AfxGetApp()->m_hInstance, NULL);

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;

            // Response to Up
            if ((pkbhs->vkCode == VK_UP) && (WM_KEYUP == wParam))
            {
                    HWND hwnd=::FindWindow(NULL, "WindowCaption");
                    ::PostMessage(hwnd, WM_COMMAND, ID_MENU_ITEM, 0L);
            }
        }
        default:
            break;
    }
    return CallNextHookEx (NULL, nCode, wParam, lParam);
}

BOOL InstallKbHook( )
 {

    if (hhookKb )
        UninstallKbHook();

    hhookKb = SetWindowsHookEx(WH_KEYBOARD_LL,
        (HOOKPROC)LowLevelKeyboardProc, AfxGetApp()->m_hInstance, NULL);

    return(hhookKb != NULL);

}

BOOL UninstallKbHook()
{

    BOOL fOk = FALSE;
    if (hhookKb ) {
        fOk = UnhookWindowsHookEx(hhookKb );
        hhookKb = NULL;
    }

    return(fOk);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值