Is there a way to use a variable as a keystroke action? Or another way to loop over many key combos?

The goal is to loop through several key combinations looking for a specific result. If that result is found the macro goes on to do additional actions like click etc. So in pseudo-code something like:

Action: Ctrl-1
If Image Found == True {
    Set Var X = TRUE
} else {
    Set Var X = FALSE
}

Action: Ctrl-2
If Image Found == True {
    Set Var X = TRUE
} else {
    Set Var X = FALSE
}

Action: Ctrl-3
If Image Found == True {
    Set Var X = TRUE
} else {
    Set Var X = FALSE
}

etc etc... Basically checking key combo Ctrl-1 through Ctrl-10.

So rather than make each check manually I figured I'd do a loop where the keystroke is a variable being incremented each time... Then I could set the loop condition on the Var X state. BUt it doesn't look like that action would accept a variable.

Ideas?

Edit: Oh, edit to clarify, the image being checked in each iteration is also different. So like Ctrl-1, now look for image A, if you found it do something, if not try Ctrl-2 and look for image B... etc etc.

It is not clear what you are trying to achieve, but I'll try a stab-in-the-dark to see if this helps:

  1. Macros can have as many different triggers at the same time as you like
  2. So, create your macro and assign all of the triggers to it.
  3. The first Action will be to get the trigger that was used:
    set myTrigger to %TriggerValue%
  4. Next use a Switch or Case action with the Variable myTrigger
    • Each Case will be the different triggers, as in:
      • ⌃1
      • ⌃2
      • etc

This simple macro will help you understand Trigger Tokens.

Example Results

image


MACRO:   Example of Trigger Tokens


#### DOWNLOAD:
<a class="attachment" href="/uploads/default/original/3X/7/c/7c331a269bbe4c2177ac50128c1911add8be9e2d.kmmacros">Example of Trigger Tokens.kmmacros</a> (2.8 KB)
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**

---



![image|475x440](upload://l7E1d33k53xyJk2MIySiiDXx5s3.jpeg)

---

Questions?

Thanks. But to clarify the trigger is irrelevant, what I’m describing is the action I’m trying to create. I want the macro to perform each of the keystrokes and then test the results. I would most likely trigger this from the menu or a palette.

You could do that using AppleScript keystroke command.
Pass the counter from a KM variable to an Execute Applescript that is in a KM Repeat loop.

The key code fo the 1 key is 18.
The app where you want the keystrokes applied must be active and frontmost.

tell application "Keyboard Maestro Engine" to set kmNum to getvariable "LoopCounter"
set kmNum to 17 + kmNum
tell application "System Events"
  key code kmNum using control down
end tell

Awesome, thanks!