Remap Win key when it's pressed alone Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
nbook
Posts: 3
Joined: 09 Aug 2023, 04:00

Remap Win key when it's pressed alone

Post by nbook » 09 Aug 2023, 04:15

Hello, I'm new to AHK and I'm stucked at a probably very simple problem. I want to remap Win to Ctrl + Home only if Win is pressed alone and other Win shortcuts (Win+Tab, Ctrl+Win+→, etc.) should not be affected. I tried the solution in this thread, it sends Ctrl+Home when press Win alone, but it also makes the start menu pops up...

Could someone please help? I'm using V2 but I won't mind switching back if V1 code is given. Thanks in advance.

User avatar
mikeyww
Posts: 27169
Joined: 09 Sep 2014, 18:38

Re: Remap Win key when it's pressed alone

Post by mikeyww » 09 Aug 2023, 05:39

Welcome to this AutoHotkey forum!

Possibly:

Code: Select all

#Requires AutoHotkey v2.0
ih := InputHook(), ih.KeyOpt('{All}', 'E')
LWin::ih.Start(), ih.Wait()
LWin Up::ih.Stop(), Send(ih.EndKey = '' ? '^{Home}' : '#{' ih.EndKey '}')
This may need adjustment for modifier combinations.

nbook
Posts: 3
Joined: 09 Aug 2023, 04:00

Re: Remap Win key when it's pressed alone

Post by nbook » 09 Aug 2023, 11:23

mikeyww wrote:
09 Aug 2023, 05:39
Welcome to this AutoHotkey forum!

Possibly:

Code: Select all

#Requires AutoHotkey v2.0
ih := InputHook(), ih.KeyOpt('{All}', 'E')
LWin::ih.Start(), ih.Wait()
LWin Up::ih.Stop(), Send(ih.EndKey = '' ? '^{Home}' : '#{' ih.EndKey '}')
This may need adjustment for modifier combinations.
Wow, works like a magic! I learnd a lot from it. Thank you again!

nbook
Posts: 3
Joined: 09 Aug 2023, 04:00

Re: Remap Win key when it's pressed alone

Post by nbook » 09 Aug 2023, 13:10

mikeyww wrote:
09 Aug 2023, 05:39
Welcome to this AutoHotkey forum!

Possibly:

Code: Select all

#Requires AutoHotkey v2.0
ih := InputHook(), ih.KeyOpt('{All}', 'E')
LWin::ih.Start(), ih.Wait()
LWin Up::ih.Stop(), Send(ih.EndKey = '' ? '^{Home}' : '#{' ih.EndKey '}')
This may need adjustment for modifier combinations.
Hmm, I actually find the behaviour is slightly different from expected. The input hook will end when Win is released so Win + Tab is not triggered instantly. And sending '#{' ih.EndKey '}' doesn't work when the end is a modifier, e.g. Win + Ctrl + → doesn't work (Ctrl + Win + → does though). I tried something like this but with no luck.

Code: Select all

#Requires AutoHotkey v2.0
ih := InputHook()
ih.KeyOpt('{All}', 'E')
ih.KeyOpt("{LCtrl}{RCtrl}{LAlt}{RAlt}{LShift}{RShift}", "-E")
LWin::ih.Start()
ih.Wait()
LWin Up::{
	Send(ih.EndKey = '' ? '^{Home}' : '#' ih.EndMods '{' ih.EndKey '}')
}
Again the current solution is good enough, I just wonder if it's possible to make it even beter?

User avatar
mikeyww
Posts: 27169
Joined: 09 Sep 2014, 18:38

Re: Remap Win key when it's pressed alone

Post by mikeyww » 09 Aug 2023, 21:20

Could try:

Code: Select all

#Requires AutoHotkey v2.0
ih := InputHook(), ih.KeyOpt('{All}', 'E')
LWin Up::ih.Stop(), Send(ih.EndKey = '' ? '^{Home}' : '{LWin up}')
LWin:: {
 ih.Start(), ih.Wait()
 If ih.EndKey != ''
  Send '{LWin down}{' ih.EndKey ' down}'
}
Test the script before you change it.

ntepa
Posts: 434
Joined: 19 Oct 2022, 20:52

Re: Remap Win key when it's pressed alone  Topic is solved

Post by ntepa » 09 Aug 2023, 22:20

Try this:

Code: Select all

#Requires Autohotkey v2.0

~LWin::Send "{Blind}{VKFF}"
~LWin up::{
    if (A_PriorKey = "LWin")
        Send "^{Home}"
}

User avatar
mikeyww
Posts: 27169
Joined: 09 Sep 2014, 18:38

Re: Remap Win key when it's pressed alone

Post by mikeyww » 10 Aug 2023, 05:14

Yeah, that makes more sense!

d_romeo
Posts: 28
Joined: 23 Dec 2020, 12:18

Re: Remap Win key when it's pressed alone

Post by d_romeo » 09 May 2024, 13:30

I tried the scripts proposed in this discussion and they work, but they interfere with the touchpad. I remapped the three-finger touch on my touchpad to ctrl + r, but when the script is running, other combinations are sent.
With:

Code: Select all

ih := InputHook()
ih.KeyOpt('{All}', 'E')
ih.KeyOpt("{LCtrl}{RCtrl}{LAlt}{RAlt}{LShift}{RShift}", "-E")
LWin::ih.Start()
ih.Wait()
LWin Up::{
	Send(ih.EndKey = '' ? '!{Space}' : '#' ih.EndMods '{' ih.EndKey '}')
}
#p is sent, after only 'r'.
With:

Code: Select all

ih := InputHook(), ih.KeyOpt('{All}', 'E')
LWin Up:: ih.Stop(), Send(ih.EndKey = '' ? '!{Space}' : '{LWin up}')
LWin:: {
    ih.Start(), ih.Wait()
    If ih.EndKey != ''
        Send '{LWin down}{' ih.EndKey ' down}'
}
is sent #p and #<

Post Reply

Return to “Ask for Help (v2)”