Need help creating a script that rotates through a set of keys but resets to the first key if any other key is pressed Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
jabronimus
Posts: 5
Joined: 26 May 2022, 08:47

Need help creating a script that rotates through a set of keys but resets to the first key if any other key is pressed

Post by jabronimus » 26 May 2022, 08:53

Title basically says it all. I'm looking for a script that will rotate through a set of keys, but if another keyboard key is clicked, the script will reset to "start over" at the first key in the sequence.

I found and have been using this script, which rotates through the set of keys well, but will always continue through the sequence, even if another key is pressed:

Code: Select all

^0::Send, % CycleAnnotate("^1,^2,^3")

CycleAnnotate(p_Input,p_Step=1,p_Delim=",",p_Omit=" `t") {
  static l_Idx := 0
  StringSplit, l_Arr, p_Input, %p_Delim%, %p_Omit%
  Return (!l_Arr0)OR((l_Idx:=Abs(1+Mod(l_Idx+p_Step-1,l_Arr0)))=0)
    ? "" : l_Arr%l_Idx%
}
Any help? Thank you! :)
Last edited by BoBo on 26 May 2022, 09:06, edited 1 time in total.
Reason: Replaced [quote] with [code]-tags for code section.

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

Re: Need help creating a script that rotates through a set of keys but resets to the first key if any other key is press

Post by Rohwedder » 26 May 2022, 10:35

Hallo,
try:

Code: Select all

#InstallKeybdHook
^0::Send,% (A_PriorKey<>"0"?CycleAnnotate(""):"") CycleAnnotate("^1,^2,^3")

CycleAnnotate(p_Input,p_Step=1,p_Delim=",",p_Omit=" `t") {
	Static l_Idx := 0
	l_Idx := p_Input=""?0:l_Idx
	StringSplit, l_Arr, p_Input, %p_Delim%, %p_Omit%
	Return (!l_Arr0)OR((l_Idx:=Abs(1+Mod(l_Idx+p_Step-1,l_Arr0)))=0)
	? "" : l_Arr%l_Idx%
}
or:

Code: Select all

#InstallKeybdHook
Keys := ["^1","^2","^3"]
^0::Send,% Keys[Mod(No:=A_PriorKey<>"0"?0:++No,Keys.Length())+1]
A_PriorKey is the last key that was released. As long as only the key 0 of the combination ^0:: is released, the rotation continues. But if the Ctrl key is also released, the rotation is reset.

jabronimus
Posts: 5
Joined: 26 May 2022, 08:47

Re: Need help creating a script that rotates through a set of keys but resets to the first key if any other key is press

Post by jabronimus » 26 May 2022, 11:33

Thank you for your response - sadly, the code isn't (yet) working for me.

The way this code works is that I have a button on my mouse assigned to ^0. As I hit the button, it cycles through ^1, ^2, and ^3 in order to change to different functions in a program I'm using.

Ideally, I would like some code that would accomplish this task even when the control key is released.

So, is there a way to accomplish this task if the control key is also released? Or is there other code that would allow for this? I'm not tied to the code I've used above - that's just something I found online that helped me get to this point.

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

Re: Need help creating a script that rotates through a set of keys but resets to the first key if any other key is press

Post by Rohwedder » 26 May 2022, 11:53

I have a button on my mouse assigned to ^0
Why don't you control the rotation directly with your mouse button?

jabronimus
Posts: 5
Joined: 26 May 2022, 08:47

Re: Need help creating a script that rotates through a set of keys but resets to the first key if any other key is press

Post by jabronimus » 26 May 2022, 12:16

The software for the mouse only allows a single key (or key combination) to be assigned to the mouse button. It doesn't have a way to rotate through keys. I chose ^0 for the mouse button, but it doesn't have to be that - I can make it something else.

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

Re: Need help creating a script that rotates through a set of keys but resets to the first key if any other key is press  Topic is solved

Post by Rohwedder » 26 May 2022, 12:41

Then try a single key, e.g. F9

Code: Select all

#InstallKeybdHook
Keys := ["^1","^2","^3"]
F9::Send,% Keys[Mod(No:=A_PriorKey<>A_ThisHotkey?0:++No,Keys.Length())+1]

jabronimus
Posts: 5
Joined: 26 May 2022, 08:47

Re: Need help creating a script that rotates through a set of keys but resets to the first key if any other key is press

Post by jabronimus » 28 May 2022, 07:55

Thanks for all your help so far ... still not working for me.

Is this code for hotkey v1 or v2? I've been using v2 so far

gregster
Posts: 8918
Joined: 30 Sep 2013, 06:48

Re: Need help creating a script that rotates through a set of keys but resets to the first key if any other key is press

Post by gregster » 28 May 2022, 07:58

jabronimus wrote:
28 May 2022, 07:55
Is this code for hotkey v1 or v2? I've been using v2 so far
Uh, the code in your initial post is definitely not v2 code...

jabronimus
Posts: 5
Joined: 26 May 2022, 08:47

Re: Need help creating a script that rotates through a set of keys but resets to the first key if any other key is press

Post by jabronimus » 28 May 2022, 08:03

I apologize, you're correct. I seem to have been using v1 this whole time.

Clearly I'm pretty new to this software ...

Post Reply

Return to “Ask for Help (v1)”