How can I create a global hotkey combination that includes the windows key?

2.1k Views Asked by At

I'm using SetWindowsHookEx to capture the keyboard in order to show a running application. I can create combinations using CTRL, ALT, SHIFT, and regular keys. However I can't create a combination using the WINDOWS key (for example, CTRL + WINDOWS + A).

I've seen articles about capturing the WINDOWS key in isolation (such as to prevent the Windows 8 start screen when a game is running), but never to create a combination.

I know it's possible to capture these combinations as software such as AutoHotKey does it.

Is SetWindowsHookEx the wrong way to do this?

1

There are 1 best solutions below

0
On

I found a similar question that contained an answer that assisted. This led me down the right path to a solution.

It appears Keyboard.Modifiers can't detect the Windows key (at least on my Win8 instance).

Instead, I had to handle the Windows key in a different way using Keyboard.IsKeyDown. This led to build a method to check whether the combination of a key pressed (from my LowLevelKeyboardProc) and the current modifier keys being pressed is the same as the defined key and modifiers in properties HotKey and HotKeyModifiers .

Here's the C# of the LowLevelKeyboardProc:

/// <summary>
/// Called by windows when a keypress occurs.
/// </summary>
/// <param name="nCode">A code the hook procedure uses to determine how to process the message. If nCode is less than zero, the hook procedure must pass the message to the CallNextHookEx function without further processing and should return the value returned by CallNextHookEx.</param>
/// <param name="wParam">The identifier of the keyboard message. This parameter can be one of the following messages: WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN, or WM_SYSKEYUP. </param>
/// <param name="lParam">A pointer to a KBDLLHOOKSTRUCT structure. </param>
private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
    if (nCode >= 0 && (wParam == (IntPtr)WM_KEYDOWN || wParam == (IntPtr)WM_SYSKEYDOWN))
    {
        int vkCode = Marshal.ReadInt32(lParam);
        var keyPressed = KeyInterop.KeyFromVirtualKey(vkCode);

        if (IsKeyCombinationPressed(keyPressed))
            OnKeyCombinationPressed(new EventArgs());
    }

    return CallNextHookEx(_hookId, nCode, wParam, lParam);
}

And the C# for the IsKeyCombinationPressed() method:

/// <summary>
/// Returns true if the registered key combination is pressed
/// </summary>
/// <remarks>
/// Keyboard.Modifiers doesn't pick up the windows key (at least on Windows 8) so we use Keyboard.IsKeyDown to detect it (if required).
/// </remarks>
bool IsKeyCombinationPressed(Key keyPressed)
{
    if (keyPressed != HotKey) return false;

    //Handle windows key
    bool isWindowsKeyRequired = (HotKeyModifiers & ModifierKeys.Windows) != 0;
    bool isWindowsKeyPressed = Keyboard.IsKeyDown(Key.LWin) || Keyboard.IsKeyDown(Key.RWin);

    //Remove windows key from modifiers (if required)
    ModifierKeys myModifierKeys = isWindowsKeyRequired ? HotKeyModifiers ^ ModifierKeys.Windows : HotKeyModifiers;
    bool isModifierKeysPressed = Keyboard.Modifiers == myModifierKeys;

    return isWindowsKeyRequired
        ? isWindowsKeyPressed && isModifierKeysPressed
        : isModifierKeysPressed;
}