Change keys only when Ctrl is pressed Topic is solved

Ask gaming related questions (AHK v1.1 and older)
User avatar
maxoku
Posts: 2
Joined: 20 Jan 2022, 23:16

Change keys only when Ctrl is pressed

Post by maxoku » 20 Jan 2022, 23:38

I need to make arrow keys to execute WASD when I hold Ctrl. Mainly to execute walking instead of running.
So I've made code:

Code: Select all

^Up::w
^Down::s
^Left::a
^Right::d
It works when I first hold Ctrl and then press an arrow key, but not when I start running and then press Ctrl.
This code:

Code: Select all

Up::w
Down::s
Left::a
Right::d
normally works for that, but then I don't run even without Ctrl pressed. I need arrows to work as arrows without Ctrl and with Ctrl work as WASD.
Is there a way to make that second code to work only when I hold Ctrl or is there any other method that will do what I need? I've tried many methods found on the web, but nothing worked.

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

Re: Change keys only when Ctrl is pressed  Topic is solved

Post by Rohwedder » 21 Jan 2022, 06:02

Hallo,
try:

Code: Select all

#InstallKeybdHook
Up::Do("{w Up}{Up Down}")
Down::Do("{s Up}{Down Down}")
Left::Do("{a Up}{Left Down}")
Right::Do("{d Up}{Right Down}")
Up Up::Do("{Up Up}{w Up}")
Down Up::Do("{Down Up}{s Up}")
Left Up::Do("{Left Up}{a Up}")
Right Up::Do("{Right Up}{d Up}")
^Up::Do("{Up Up}{w Down}")
^Down::Do("{Down Up}{s Down}")
^Left::Do("{Left Up}{a Down}")
^Right::Do("{Right Up}{d Down}")
^Up Up::Do("{Up Up}{w Up}")
^Down Up::Do("{Down Up}{s Up}")
^Left Up::Do("{Left Up}{a Up}")
^Right Up::Do("{Right Up}{d Up}")
~Ctrl Up::
~Ctrl::
IF Do
	SetTimer,% ["^"][GetKeyState("Ctrl","P")] Do, -50
Return
Do(This)
{	Global
	IF (A_ThisHotkey = A_ThisLabel)
		Do := StrReplace(A_ThisHotkey,"^")
	Else IF SubStr(Do, -2) = " Up"
		Do =
	SendInput,% This	
}

User avatar
maxoku
Posts: 2
Joined: 20 Jan 2022, 23:16

Re: Change keys only when Ctrl is pressed

Post by maxoku » 21 Jan 2022, 12:16

Thanks, I've seen the previous code and it works, though only in a separate script. I've just finished up setting everything and returned to thank you. I've checked this new code and it's better, works in my main script. Works perfectly. Thanks a lot.

Post Reply

Return to “Gaming Help (v1)”