Page 1 of 1

Cycling keystrokes?

Posted: 16 May 2018, 15:33
by tgoVIPER
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,

Re: Cycling keystrokes?

Posted: 16 May 2018, 15:41
by swagfag

Code: Select all

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

F7::Send % cycle(3, 3)
Esc::ExitApp

Re: Cycling keystrokes?

Posted: 16 May 2018, 16:12
by wolf_II
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.

Re: Cycling keystrokes?

Posted: 17 May 2018, 09:48
by Kryu
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)

Re: Cycling keystrokes?

Posted: 17 May 2018, 11:20
by swagfag
this isnt practice, this is abuse lol

Re: Cycling keystrokes?

Posted: 17 May 2018, 11:30
by Kryu
swagfag wrote:this isnt practice, this is abuse lol
I agree. :D

Re: Cycling keystrokes?

Posted: 17 May 2018, 22:00
by tgoVIPER
Haha!
Thanks, everyone.
They all work great and I appreciate the help.

Re: Cycling keystrokes?

Posted: 18 May 2018, 01:22
by Xtra
Kryu wrote: but just to practice ternary operator:

Code: Select all

F7::Send % c := c=3 ? 4 : c=4 ? 5 : 3

Re: Cycling keystrokes?

Posted: 18 May 2018, 07:00
by Kryu
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: