Blocking windows hotkeys in an application

Ash 40 Reputation points
2023-05-17T11:14:26.54+00:00

I'm creating a C++ application wherein I need to programmatically block several windows hotkeys. I'm using SetWindowsHook() and lowlevelkeyboardproc() winapi calls. But just returning a non-zero value for the hook does not block the following keys. 1) Windows key + L 2) Windows Key + alt + R 3) Windows key + G 4) Windows key + K and a few more sequences with windows key. I have tried several ways but none of them seem to work. So I wanted to know if it is not possible to override the system hotkeys in Windows 7 and Windows 10. Help much appreciated!

Windows 10
Windows 10
A Microsoft operating system that runs on personal computers and tablets.
10,576 questions
Windows
Windows
A family of Microsoft operating systems that run across personal computers, tablets, laptops, phones, internet of things devices, self-contained mixed reality headsets, large collaboration screens, and other devices.
4,724 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 81,461 Reputation points
    2023-05-19T16:30:35.4333333+00:00

    Some keys combinations cannot be intercepted by a LL Keyboard Hook

    But they can be disabled with Registry.

    For example :

    Windows Key + L :

    HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System

    DisableLockWorkstation = 1

    Windows Key + G ( + Windows Key + Alt + R and other shortcuts for Xbox Game Bar)

    HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\GameDVR

    AppCaptureEnabled = 0

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Limitless Technology 43,926 Reputation points
    2023-05-18T13:54:55.1733333+00:00
    Hello Ash,
    
    Thank you for your question and for reaching out with your question today.
    
    Please see the code example below that uses a system-wide hook without the need for DLL interaction:
    
    LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
    {
    if (nCode < 0)
        return CallNextHookEx(NULL, nCode, wParam, lParam);
    
    tagKBDLLHOOKSTRUCT *str = (tagKBDLLHOOKSTRUCT *)lParam;
    
    switch(str->flags)
    {
        case (LLKHF_ALTDOWN):
            qDebug() << "ALT";
            delete str;
        return 1;
    }
    
    if (wParam == WM_KEYDOWN)
    {
        switch (str->vkCode)
        {
            case VK_RWIN:
            case VK_LWIN:
            case VK_LCONTROL:
            case VK_RCONTROL:
            case VK_APPS:
            case VK_SLEEP:
            case VK_MENU:
                qDebug() << "SPECIAL PRESS";
                delete str;
            return 1;
        }
    }
    
    return CallNextHookEx(NULL, nCode, wParam, lParam);
    }
    
    It blocks inputs of Ctrl, Windows Key and Alt.
    
    If the reply was helpful, please don’t forget to upvote or accept as answer.
    
    0 comments No comments