cycling between keys

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
coinmaster

cycling between keys

06 Dec 2015, 00:30

Hi, I'm trying to figure out how to set up autohotkey to cycle between keys for each keypress.
For example if I press "1" it will use 1, if I press "1" again it will use 2, if I press "1" again it will use 3.
I do not want this to be automated, I want to manually cycle between keys with each press.
Any ideas?
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: cycling between keys

06 Dec 2015, 01:52

This is a more basic approach, so it's longer code. Others might suggest to you to use arrays, which can be shorter.

Code: Select all

$1:: ; the $ prevents recursion
counter++ ; counter is a custom variable
If counter=1
   Send 1
If counter=2
   Send 2
If counter=3
   Send 3
return
$ modifier, variables

But in this particular use case, you could consider sending the variable itself:

Code: Select all

$1::
counter++
Send %counter%
return
coinmaster

Re: cycling between keys

06 Dec 2015, 03:14

I actually want it to reset to 1 after 9.
Currently it stops at 3 with the first one you listed, or it goes to infinity with the second one you listed.
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: cycling between keys

06 Dec 2015, 03:23

Yep, you can reset it. I'll let you modify that part. The first of course only goes to three because I only gave options for it to do an action up through 3. You're free to give it options up through 9. As for resetting it, you can assign counter a new value by using the := (or =) operator. counter:=0. (Follow that link and read the differences between := and = if you're curious, as there is a difference.)
coinmaster

Re: cycling between keys

06 Dec 2015, 03:38

Sorry, I'm having a difficult time understanding where counter:= is supposed to go in order to reset it.
Forgive me I'm new to this.
coinmaster

Re: cycling between keys

06 Dec 2015, 03:57

":=" seems to override the rest of the scipt and just send whatever the value attached to it is and = will just add more numbers to the counter.
I think you might be thinking I want the script to run up to a defined number of sends and then stop but I want it to infintitly repeat 1-9.
coinmaster

Re: cycling between keys

06 Dec 2015, 03:58

":=" seems to override the rest of the scipt and just send whatever the value attached to it is and = will just add more numbers to the counter.
I think you might be thinking I want the script to run up to a defined number of sends and then stop but I want it to infintitly repeat 1-9.
coinmaster

Re: cycling between keys

06 Dec 2015, 05:39

Is there some way to make it reset after hitting return?
I've spent the last couple hours trying to figure out a solution but I can't seem to find anything that works.
coinmaster

Re: cycling between keys

06 Dec 2015, 06:53

I found a thread from 2009 that said this should work

$1::
if count >= 9
count=0

counter++

If counter=1
Send 1
If counter=2
Send 2
If counter=3
Send 3
If counter=4
Send 4
If counter=5
Send 5
If counter=6
Send 6
If counter=7
Send 7
If counter=8
Send 8
If counter=9
Send 9

return

but it still stops counting at 9.
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: cycling between keys

06 Dec 2015, 06:59

Code: Select all

count:=0,Array:=["1","2","3","4","5","6","7","8","9"]
$1::Send % Array[(++count > 9) ? count:=1 : count]
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: cycling between keys

06 Dec 2015, 12:23

For your attempt just before HotKeyIt's post, notice there are two variables used - count and counter. Making them all one variable should have made it work.

My intention would have been something like this:

Code: Select all

$1::
counter++
Send %counter%
If counter=9
counter:=0
return
Peabianjay
Posts: 52
Joined: 07 Nov 2015, 22:50

Re: cycling between keys

06 Dec 2015, 12:45

Alternatively:

Code: Select all

$1::
    send, % mod(counter++, 10 )
return

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], Xaucy and 162 guests