winapiui-automationkeyboard-hookmouse-hook

Windows API Mousehook , Capture rightmousebutton + Ctrl (WM_RBUTTONDOWN + MK_CONTROL) clicked togather


initially i was able to print something when i pressed only right mouse button using if (wParam == WM_RBUTTONDOWN)but now , i want the same effect, i want to print something when right mouse button + Ctrl key is pressed. how can i acheive that ?

i have tried this


LRESULT CALLBACK MainWindow::mouseProc(int Code, WPARAM wParam, LPARAM lParam)
{
        auto& ms = *(const MSLLHOOKSTRUCT*)lParam;
        MSLLHOOKSTRUCT* pMouseStruct = (MSLLHOOKSTRUCT*)lParam;
        if (pMouseStruct != nullptr)
        {
            if (wParam == WM_RBUTTONDOWN & MK_CONTROL)  // Here, i added MK_CONTROL but it doesn't work
            {
                qDebug() << "Print something when Right mouse button and Ctrl button is pressed togather"; 
            }
        }
    return CallNextHookEx(NULL, Code, wParam, lParam);
}

UPDATE

when i want to try the case where only Ctrl is pressed and it should print something, it still doesn't work

LRESULT CALLBACK MainWindow::mouseProc(int Code, WPARAM wParam, LPARAM lParam)
{
        auto& ms = *(const MSLLHOOKSTRUCT*)lParam;
        MSLLHOOKSTRUCT* pMouseStruct = (MSLLHOOKSTRUCT*)lParam;
        if (pMouseStruct != nullptr)
        {
            if (wParam == MK_CONTROL)  // Here, i added only MK_CONTROL but it doesn't work
            {
                qDebug() << "Print something when  Ctrl button is pressed ";
            }
        }
    return CallNextHookEx(NULL, Code, wParam, lParam);
}

what am i missing here ?


Solution

  • First of all, if you want to capture the right button + ctrl, you can check the state of the Ctrl key (whether it is pressed) when WM_RBUTTONDOWN is detected.

    LRESULT CALLBACK mouseProc(int Code, WPARAM wParam, LPARAM lParam)
    {
        auto& ms = *(const MSLLHOOKSTRUCT*)lParam;
        MSLLHOOKSTRUCT* pMouseStruct = (MSLLHOOKSTRUCT*)lParam;
        if (pMouseStruct != nullptr)
        {
            if (wParam == WM_RBUTTONDOWN && (GetAsyncKeyState(VK_LCONTROL)&0x8000))  //Left CONTROL key as example
            {
                std::cout << "ctrl + rbutton";
            }
        }
        return CallNextHookEx(NULL, Code, wParam, lParam);
    }
    

    If you want to use a keyboard hook to hook only "Ctrl":

    LRESULT CALLBACK keyboardProc(int Code, WPARAM wParam, LPARAM lParam)
    {
        KBDLLHOOKSTRUCT* pKeyboardStruct = (KBDLLHOOKSTRUCT*)lParam;
        if (pKeyboardStruct != nullptr)
        {
            if (pKeyboardStruct->vkCode == VK_LCONTROL)  
            {
                if(wParam == WM_KEYDOWN)
                    std::cout << "  -ctrl-  ";
            }
        }
        return CallNextHookEx(NULL, Code, wParam, lParam);
    }
    void main(void)
    {
        HHOOK hmouse = SetWindowsHookEx(WH_MOUSE_LL, mouseProc, hInstance, 0);
        HHOOK hkeyboard = SetWindowsHookEx(WH_KEYBOARD_LL, keyboardProc, hInstance, 0);
    
        MSG msg;
        while (GetMessage(&msg, 0, 0, 0))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        UnhookWindowsHookEx(hmouse);
        UnhookWindowsHookEx(hkeyboard);
    
        return;
    };