Help me implement a sequence of keypresses more elegantly Topic is solved

Ask gaming related questions (AHK v1.1 and older)
User avatar
sh00ter666
Posts: 18
Joined: 07 Oct 2016, 10:55

Help me implement a sequence of keypresses more elegantly

Post by sh00ter666 » 08 Jul 2017, 02:34

Hey, up until now I used to write my hotkey script squences like so:

Code: Select all

#NoEnv
SendMode Input  
SetWorkingDir %A_ScriptDir%  

$j::
SendInput {e down}
Sleep 50
SendInput {e up}

Sleep 55

SendInput {left down}
Sleep 50
SendInput {left up}

Sleep 55

SendInput {up down}
Sleep 50
SendInput {up up}

Sleep 55

SendInput {y down}
Sleep 50
SendInput {y up}

Sleep 55


SendInput {down down}
Sleep 50
SendInput {down up}

Sleep 55

SendInput {y down}
Sleep 50
SendInput {y up}


return
This is highly inefficient if I want to change something around. And as you can notice it's always holding a key down for the same amount of time and the Sleep in between is the same as well. If I want to change one of these values for all of them, I'd have to make dozens of edits.

Can someone maybe help me with a function that could be called something like this

Code: Select all

SendSequence(e,left,y,up,y,right,right,y,down,y)
for example? I want to use this in a game which is why the hold-down is important because else the keys won't be registered :shh:

User avatar
YoucefHam
Posts: 372
Joined: 24 Aug 2015, 12:56
Location: Algeria
Contact:

Re: Help me implement a sequence of keypresses more elegantly

Post by YoucefHam » 08 Jul 2017, 02:47

Code: Select all

#NoEnv
SendMode Input  
SetWorkingDir %A_ScriptDir%  

$j::
keys = e|Left|Up|y|Down|y
Loop, Parse, % keys, |
{
	Sleep 55
	SendInput {%A_LoopField% down}
	Sleep 50
	SendInput {%A_LoopField% up}
}return
:wave: There is always more than one way to solve a problem. ;)

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Help me implement a sequence of keypresses more elegantly

Post by BoBo » 08 Jul 2017, 02:48

Code: Select all

#SingleInstance, Force

If (A_ScriptName = "SendKey().ahk"){                   ; for testing change this setting to your scripts name. That way you can #Include it within other scripts without further changes.
    SendKey("x",34)                                    ; call the function to send the single key "x" with a click delay of 34ms 
    SendSeq("up,e,left,y,up,y,right,right,y,down,y")   ; call the function to send a sequence of keystrokes with a default click delay of 50ms
    ExitApp
    }

SendSeq(keyIndex){
    key := StrSplit(keyIndex,",")
    Loop,% key.MaxIndex()
       SendKey(key[A_Index])
    }

SendKey(key,delay=500){                                ; for testing click delay has been slowed down to 500ms, default would be 50ms. Change it to your liking.
    SendInput, {%key% down}
    Sleep, % delay
    SendInput, {%key% up}
    }
Last edited by BoBo on 09 Jul 2017, 02:12, edited 8 times in total.

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

Re: Help me implement a sequence of keypresses more elegantly  Topic is solved

Post by Rohwedder » 08 Jul 2017, 02:54

Hallo,
my way:

Code: Select all

q::
SendSequence("e,left,y,up,y,right,right,y,down,y")
Return

SendSequence(Sequence)
{
	Loop, Parse, Sequence, CSV
	{
		SendInput, {%A_LoopField% Down}
		Sleep 50
		SendInput, {%A_LoopField% Up}
	}
}

Guest

Re: Help me implement a sequence of keypresses more elegantly

Post by Guest » 08 Jul 2017, 03:01

Code: Select all

#NoEnv
SendMode Input  
SetWorkingDir, % A_ScriptDir

$j::SendSequence("e", "Left", "Up", "y", "Down", "y")

SendSequence(Keys*){
	Loop, % Keys.Length()
	{
		Sleep 55
		Send, % "{" Keys[A_Index] " Down}"
		Sleep 50
		Send, % "{" Keys[A_Index] " Up}"
	}
Return
}

Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Help me implement a sequence of keypresses more elegantly

Post by Helgef » 08 Jul 2017, 03:18

Another suggestion,

Code: Select all

j::
	SetKeyDelay, 55, 50
	SendEvent,e{left}{up}y{down}y
return

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Help me implement a sequence of keypresses more elegantly

Post by BoBo » 08 Jul 2017, 03:19

Helgef wrote:Another suggestion,

Code: Select all

j::
	SetKeyDelay, 55, 50
	SendEvent,e{left}{up}y{down}y
return
:clap: :clap: :clap: :thumbup:

User avatar
sh00ter666
Posts: 18
Joined: 07 Oct 2016, 10:55

Re: Help me implement a sequence of keypresses more elegantly

Post by sh00ter666 » 08 Jul 2017, 03:21

Thanks a thousand times to everyone who has helped so incredibly quickly!!!

I am currently using Rohwedder's approach and it's working like a charm! :D :rainbow:

BoBo wrote:
Helgef wrote:Another suggestion,

Code: Select all

j::
 SetKeyDelay, 55, 50
 SendEvent,e{left}{up}y{down}y
return
:clap: :clap: :clap: :thumbup:

Ahhh so this is how you are supposed to use those commands! :lol: I will definitely keep it in mind, although Rohwedder's solution is still a little more convenient because of not having to type the curly brackets for up, down, left, right :dance:

User avatar
evilC
Posts: 4824
Joined: 27 Feb 2014, 12:30

Re: Help me implement a sequence of keypresses more elegantly

Post by evilC » 10 Jul 2017, 06:16

eww, why split strings like that? What a waste of CPU!
Guest's method seems partially better, but a variadic function also seems overkill.

Code: Select all


SendSequence(["{e}", "{left}", "{y}" , "{up}", "{y}", "{right}", "{right}", "{y}", "{down}", "{y}"])
return

SendSequence(arr){
	for i, key in arr {
		Send % key
	}
}

Post Reply

Return to “Gaming Help (v1)”