Re-assign/override hotkey (Win + L) to lock windows

前端 未结 6 1087
悲&欢浪女
悲&欢浪女 2020-12-08 15:04

Is it possible to re-assign the Win+L hotkey to another executable/shortcut?

Use-case - I would like to switch off the monitor of my laptop as

6条回答
  •  余生分开走
    2020-12-08 15:29

    The Win+L is a system assigned hotkey and there's no option to disable it. This means there's no way for an application to detect it, unless you use a low-level global keyboard hook (WH_KEYBOARD_LL). This works in XP SP3; haven't tested it in Vista though:

    LRESULT CALLBACK LowLevelKeyboardProc(int code, WPARAM wparam, LPARAM lparam) {
        KBDLLHOOKSTRUCT& kllhs = *(KBDLLHOOKSTRUCT*)lparam;
        if (code == HC_ACTION) {
            // Test for an 'L' keypress with either Win key down.
            if (wparam == WM_KEYDOWN && kllhs.vkCode == 'L' && 
                (GetAsyncKeyState(VK_LWIN) < 0 || GetAsyncKeyState(VK_RWIN) < 0))
            {
                // Place some code here to do whatever you want.
                // ...
    
                // Return non-zero to halt message propagation
                // and prevent the Win+L hotkey from getting activated.
                return 1;
            }
        }
        return CallNextHookEx(0, code, wparam, lparam);
    }
    

    Note that you need a low-level keyboard hook. A normal keyboard hook (WH_KEYBOARD) won't catch hotkey events.

提交回复
热议问题