Skip to content

Instantly share code, notes, and snippets.

@Arax20
Last active August 18, 2024 15:16
Show Gist options
  • Save Arax20/9493799b3aa73e1fb69af8d4f1c72ba6 to your computer and use it in GitHub Desktop.
Save Arax20/9493799b3aa73e1fb69af8d4f1c72ba6 to your computer and use it in GitHub Desktop.
Allows rebinding of winkey to a shortcut without getting in the way of system winkey shortcuts.
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
LWin::
Input, x, L1 ; Catches key after winkey
if (ErrorLevel = "NewInput") ; If catching was interrupted
{
Send #+r ; Set this to your powertoys run shortcut
return
}
Send, #%x% ; Sends caught key + winkey
return
~LWin Up::
Input, , L0 ; Interrupts key catching on winkey release
return
@GageSorrell
Copy link

As someone who isn't familiar with the AHK language, I struggled with modifying the Send line to an appropriate shortcut that matched what I set in the PowerToys app.

What worked for me was to set the keyboard shortcut in PowerToys to Win + Control + Alt + A. The line Send #+r then becomes

Send #^!a

@BlakeMpy
Copy link

Currently having some issues with a bit of an edge case here. Windows currently uses higher function keys to handle events like touchpad taps, with 3 and 4 fingered taps translating to Win+Ctrl+Shift+F22 and Win+Ctrl+Shift+F24, respectively. For some reason the script seems to open up Run despite the extra keys being pressed. Perhaps they are too fast for AHK to deal with:

VK  SC  Type      Up/Dn     Elapsed       Key
74  03F	 	  d	    0.38	  F5             	// Me refreshing ahk key list
74  03F	 	  u	    0.09	  F5             	
74  03F	 	  d	    0.38	  F5             	
74  03F	 	  u	    0.14	  F5             	
5B  05B	h	  d	    2.56	  LWin           	// Performing 3-fingered tap on touchpad
A2  01D	a	  d	    0.00	  LControl       	
A0  02A	a	  d	    0.02	  LShift         	
85  06D	a	  d	    0.00	  F22            	
85  06D	a	  u	    0.00	  F22            	
A0  02A	a	  u	    0.00	  LShift         	
A2  01D	a	  u	    0.00	  LControl       	
5B  05B	h	  u	    0.00	  LWin           	
5B  15B	i	  d	    0.00	  LWin           	// ahk begins pressing my Run shortcut (Win+Space)
20  039	i	  d	    0.00	  Space          	
FF  000	a	  u	    0.00	  not found      	
20  039	i	  u	    0.03	  Space          	
5B  15B	i	  u	    0.00	  LWin           	

Not entirely certain how to go about fixing, but I'll experiment a little and see if I can find anything helpful

@konradsemurai
Copy link

konradsemurai commented Aug 18, 2024

LWin::
{
    ; Wait for 0.5 seconds (500 milliseconds)
    KeyWait("LWin", "T0.5")
    if !GetKeyState("LWin", "P") ; If the key is released before 0.5 seconds
    {
        Send("{Alt Down}{Space}{Alt Up}") ; Send Alt + Space
        Return
    }

    ; If the key is still held down after 0.5 seconds, allow normal behavior
    Send("{LWin Down}")
    KeyWait("LWin") ; Wait until the key is released
    Send("{LWin Up}")
    Return
}

Would love a better solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment