Hold modifier until next keypress

Ask gaming related questions (AHK v1.1 and older)
pharaohoo
Posts: 3
Joined: 22 Jul 2021, 06:41

Hold modifier until next keypress

Post by pharaohoo » 22 Jul 2021, 07:18

Hello!
I am looking a way to make modifier key hold down until next keypress and be released on it.
Let's say in game I have 8 buttons from 1-8 and I would like to use them as Ctrl+1-8 ; Shift+1-8 and Alt+1-8 but I would need to activate them without holding modifier keys physicals down.
I would like to press Ctrl and them press 1 and it will make Ctrl+1, but in same time it should release Ctrl key.
Best I have found at the moment is this script:

Code: Select all

$Ctrl::
	Send, {Ctrl down}
	KeyWait, Ctrl
	Sleep, 500
	Send, {Ctrl up}
return

$Alt::
	Send, {Alt down}
	KeyWait, Alt
	Sleep, 500
	Send, {Alt up}
return

$Shift::
	Send, {Shift down}
	KeyWait, Shift
	Sleep, 500
	Send, {Shift up}
return
[Mod edit: [code][/code] tags added.]
It is not the same I want, but at least I can make it work for now. Thank You

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

Re: Hold modifier until next keypress

Post by mikeyww » 22 Jul 2021, 08:58

Code: Select all

^1::
^2::
last := hk, hk := SubStr(A_ThisHotkey, 2)
If (hk = last && on) {
 Send {Ctrl up}
 on := False
 Return
} Else Send {Ctrl down}%hk%
on := True
Return

pharaohoo
Posts: 3
Joined: 22 Jul 2021, 06:41

Re: Hold modifier until next keypress

Post by pharaohoo » 22 Jul 2021, 11:28

mikeyww wrote:
22 Jul 2021, 08:58

Code: Select all

^1::
^2::
last := hk, hk := SubStr(A_ThisHotkey, 2)
If (hk = last && on) {
 Send {Ctrl up}
 on := False
 Return
} Else Send {Ctrl down}%hk%
on := True
Return
Thank You for reply. My Ctrl button stop working at all after started this script. :shock:

Rohwedder
Posts: 7510
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Hold modifier until next keypress

Post by Rohwedder » 22 Jul 2021, 11:30

Hallo,
try:

Code: Select all

Ctrl::
Alt::
Shift::
Send, {%A_ThisHotkey% Down}
KeyWaitAny("V")
Send, {%A_ThisHotkey% Up}
Return

KeyWaitAny(Options:="")
{ ;https://www.autohotkey.com/docs/commands/InputHook.htm#ExKeyWaitAny
    ih := InputHook(Options)
    if !InStr(Options, "V")
        ih.VisibleNonText := false
    ih.KeyOpt("{All}", "E")  ; End
    ih.Start()
    ErrorLevel := ih.Wait()  ; Store EndReason in ErrorLevel
    return ih.EndKey  ; Return the key name
}

pharaohoo
Posts: 3
Joined: 22 Jul 2021, 06:41

Re: Hold modifier until next keypress

Post by pharaohoo » 22 Jul 2021, 11:58

Rohwedder wrote:
22 Jul 2021, 11:30
Hallo,
try:

Code: Select all

Ctrl::
Alt::
Shift::
Send, {%A_ThisHotkey% Down}
KeyWaitAny("V")
Send, {%A_ThisHotkey% Up}
Return

KeyWaitAny(Options:="")
{ ;https://www.autohotkey.com/docs/commands/InputHook.htm#ExKeyWaitAny
    ih := InputHook(Options)
    if !InStr(Options, "V")
        ih.VisibleNonText := false
    ih.KeyOpt("{All}", "E")  ; End
    ih.Start()
    ErrorLevel := ih.Wait()  ; Store EndReason in ErrorLevel
    return ih.EndKey  ; Return the key name
}
Thank You for Your answer. This script worked, but not every time, some times I did need press few times to make it press Ctrl+1 In games it is critical if I cant use right skill in time I need.

Post Reply

Return to “Gaming Help (v1)”