Page 1 of 1

Remap Win key when it's pressed alone

Posted: 09 Aug 2023, 04:15
by nbook
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.

Re: Remap Win key when it's pressed alone

Posted: 09 Aug 2023, 05:39
by mikeyww
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.

Re: Remap Win key when it's pressed alone

Posted: 09 Aug 2023, 11:23
by nbook
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!

Re: Remap Win key when it's pressed alone

Posted: 09 Aug 2023, 13:10
by nbook
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?

Re: Remap Win key when it's pressed alone

Posted: 09 Aug 2023, 21:20
by mikeyww
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.

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

Posted: 09 Aug 2023, 22:20
by ntepa
Try this:

Code: Select all

#Requires Autohotkey v2.0

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

Re: Remap Win key when it's pressed alone

Posted: 10 Aug 2023, 05:14
by mikeyww
Yeah, that makes more sense!

Re: Remap Win key when it's pressed alone

Posted: 09 May 2024, 13:30
by d_romeo
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 #<