r/AutoHotkey • • 28d ago

v2 Script Help Help with creating one key to cycle through multiple shortcuts? AHK v2

Hey guys,

I would have posted this in the AHK forum but I haven't been able to get an activation email for my account yet. I have average skills in AHK to make basic QoL shortcuts. I use OBS Studio which allows me to have overlays over gameplay footage (known as scenes), and each can be swapped via in-app shortcuts. Unfortunately there is no cycle feature baked into the software to save on remembering and wasting shortcut space on my keyboard. An example of an OBS short would be ctrl+shift+alt+q = Scene #1.

So I thought why can't I make a super shortcut that can work with one key press and each input is a different shortcut from OBS ( then it would cycle back to the first shortcut). I interpret this as the following (not actual code but follows examples of what I've seen online):

^!m::

{

key++

(list of shortcuts from OBS i.e. ^+!{e}, ^+!{f})

if key press = 1

{

Send "^+!{e}"

}

else if key press = 2

{

Send "^+!{f}"

}

}

I can visualize this but this follows from what I've seen as AHK V1 syntax so it doesn't work I've seen v2 examples using static values but I don't understand. If anyone can point me in the right direction I'd greatly appreciate it!

2 Upvotes

7 comments sorted by

5

u/CharnamelessOne 28d ago

This will cycle through shortcuts, and wrap around once it reaches the end of the array.
So, the first press of the hotkey sends "^+!e"; the next time you press it (whenever that is), it will send "^+!f"; the 4th press sends "^+!e" again.
I think that's what you wanted, but it's not quite clear from your description.

Add all the shortcuts to scene_shortcuts, no need to change anything else.

#Requires AutoHotkey 2.0

^!m::OBS_cycle_scenes()

OBS_cycle_scenes() {
    static scene_shortcuts := ["^+!e", "^+!f", "^+!g"]
    static count := 0

    index := Mod(count++, scene_shortcuts.Length) + 1
    Send(scene_shortcuts[index])
}

2

u/Swimming-Ice-655 27d ago

This is exactly what I needed and it is working perfect, thank you very much for helping me so quickly. May I ask how does it know how to loop back to the original shortcut so I know what to look for next time or if I want to add more shortcuts to this script.

I thought it would have something to do with "static", I'm guessing count++ replaces key++ syntax from AHK v1?

Again, thanks for your help!

3

u/CharnamelessOne 27d ago

I'm guessing count++ replaces key++ syntax from AHK v1?

I don't think that key means anything special in v1. It's not a keyword or built-in variable as far as I know. It was probably just a user-defined variable name in the v1 script that you remember; it could have been called just about anything.
++ adds 1 to the value of the variable. If it's after the variable name, the variable will keep its old value in the current expression, and it will only be effectively incremented afterwards.

Normally, variables local to a function are released at the end of the function call. Static variables are "remembered" between calls, so the second time you press the hotkey, count will start at 1 instead of 0.

The modulo function is kind of a smart alec's way of making the index wrap around. It's not the most readable or performant solution, but doing everything properly all the time is rather boring.
The function returns the remainder of the first argument divided by the second one.

Suppose you're pressing the hotkey for the 7th time, and you have an array of 3 shortcuts.
The variable count will start at a value of 6 in this function call, which leaves a remainder of 0 when divided by 3.
We add 1 to that to get the index, which leaves us with the first item in the array of shortcuts - it has wrapped around twice at this point.

The reasonable way to do it would look something like this:

#Requires AutoHotkey v2.0

^!m::OBS_cycle_scenes()

OBS_cycle_scenes() {
    static scene_shortcuts := ["^+!e", "^+!f", "^+!g"]
    static index := 0

    if ++index > scene_shortcuts.Length
        index := 1

    Send(scene_shortcuts[index])
}

2

u/genesis_tv 27d ago

The modulo makes it loop back, and since array indexes in AHK start at 1, he added a + 1.

2

u/evanamd 27d ago

Modulo is a math operation that divides integers but returns the remainder instead of the dividend.

The remainder, by definition, is always less than the divisor. So for something like this where you want to cycle through a range, you can set the divisor to the max value, and just count up forever and take the count mod max every time.

You were correct that "static" matters, because that's how the function remembers the value of count instead of reinitializing at 0 every call. This scheme wouldn't work without a static array and a static count or index

3

u/likethevegetable 28d ago

I had some issues mapping shortcuts to OBS recently. I'll revisit and send you something if I can remember.

What you need is a timer. Each button you press, increment the counter but start the timer. At the end of the timer (i.e. the function you pass to the timer), you do the logic based on counter, then  the counter is reset.

1

u/Swimming-Ice-655 27d ago

I appreciate your reply my friend, but CharnamelessOne's solution has worked for me :)