Cycling keystrokes?

Ask gaming related questions
tgoVIPER
Posts: 12
Joined: 22 Apr 2018, 10:28

Cycling keystrokes?

Post by tgoVIPER » 16 May 2018, 15:33

Does anyone have a quick piece of code to assign cycling of keystrokes to one key?
i.e. I'd like to press, say, the F7 key and each time I do it cycles between sending different single keys.

example:
I press F7 the first time // AHK sends '3'
I press F7 the second time // AHK sends '4'
I press F7 a third time // AHK sends '5'

then it resets so that if you pressed it a fourth time, it would start the above process over.

I'm at work right now and can't install AHK to give it a go here.

Thanks in advance,
swagfag
Posts: 1198
Joined: 11 Jan 2017, 17:59

Re: Cycling keystrokes?

Post by swagfag » 16 May 2018, 15:41

Code: Select all

cycle(start, step) {
	static i := 0
	return Mod((++i - 1), step) + start
}

F7::Send % cycle(3, 3)
Esc::ExitApp
wolf_II
Posts: 2142
Joined: 08 Feb 2015, 20:55

Re: Cycling keystrokes?

Post by wolf_II » 16 May 2018, 16:12

One Two more:
First with ByRef parameter:

Code: Select all

Current := 0
F7:: Send, % Cycle(Current)
Cycle(ByRef Count) {
    Return, Count -= (++Count > 3) ? 3 : 0
}
And, for the heck of it, one with super-global variable:

Code: Select all

global Count := 0
F7:: Send, % Cycle()
Cycle() {
    Return, Count -= (++Count > 3) ? 3 : 0
}
I hope that helps.
Kryu
Posts: 7
Joined: 26 Sep 2016, 13:25

Re: Cycling keystrokes?

Post by Kryu » 17 May 2018, 09:48

It's ugly as hell, but just to practice ternary operator:

Code: Select all

F7::Send % (c = 5) ? (c := 3) : (c = 3) ? (c := 4) : (c = 4) ? (c := 5) : (c := 3)
swagfag
Posts: 1198
Joined: 11 Jan 2017, 17:59

Re: Cycling keystrokes?

Post by swagfag » 17 May 2018, 11:20

this isnt practice, this is abuse lol
Kryu
Posts: 7
Joined: 26 Sep 2016, 13:25

Re: Cycling keystrokes?

Post by Kryu » 17 May 2018, 11:30

swagfag wrote:this isnt practice, this is abuse lol
I agree. :D
tgoVIPER
Posts: 12
Joined: 22 Apr 2018, 10:28

Re: Cycling keystrokes?

Post by tgoVIPER » 17 May 2018, 22:00

Haha!
Thanks, everyone.
They all work great and I appreciate the help.
User avatar
Xtra
Posts: 1218
Joined: 02 Oct 2015, 12:15

Re: Cycling keystrokes?

Post by Xtra » 18 May 2018, 01:22

Kryu wrote: but just to practice ternary operator:

Code: Select all

F7::Send % c := c=3 ? 4 : c=4 ? 5 : 3
Kryu
Posts: 7
Joined: 26 Sep 2016, 13:25

Re: Cycling keystrokes?

Post by Kryu » 18 May 2018, 07:00

Xtra wrote:
Kryu wrote: but just to practice ternary operator:

Code: Select all

F7::Send % c := c=3 ? 4 : c=4 ? 5 : 3
Thanks! :thumbup:
Post Reply

Return to “Gaming”