keyboardkeyboard-shortcutsautohotkeyautohotkey-2

How to remap window key press to a shortcut


I would like to use the window key to open an app launcher, which has its own shortcut (alt + space), while also preserving the combination shortcuts (win + 1, win + d, win + tab, etc.). I have tried many different approaches, but have not been successful. My most successful attempt looks like this:

#Requires AutoHotkey v2.0

$LWin up::
{    
    if A_PriorKey = "LWin"
        Send "!{Space}"
}

This code allows me to open the app launcher, but all other shortcuts stop working. What seems to be happening is that this hook also consumes the LWin key when it's pressed down (meaning the operating system doesn't register the key press).

I would like to react only to the key release event and leave the key press event untouched. In most cases, I think I’m fine with consuming the key-up event, but it would be great if I could consume it only when the if condition is fulfilled.

I'm not tied to AutoHotkey; I installed it just for this one task. I normally use PowerToys for regular remapping. If you have another solution that could help, I'm happy to switch to it.


Solution

  • While this answer is useful, it has a flaw.
    If, for example, you press the Win + 1 hotkey and keep holding the Win key for some time after releasing the 1 key, and then release Win, alt + space would still be sent.
    I added a timer so that alt + space won't be sent if the up event happens 250ms or more after you first pressed the key.

    global startTime := -1
    
    ~LWin:: {
            global startTime
    
            Send "{Blind}{vkE8}"
    
            if startTime = -1
                    startTime := A_TickCount
    }
    
    LWin up:: {
            global startTime
    
            if A_PriorKey = "LWin" and (A_TickCount - startTime) < 250
                    Send "!{Space}"
    
            startTime := -1
    }