Need help to bulid a macro that alternantes bettween 2 different keystrokes

Need to bulid a macro that alternates between sending Command+r and Command+p.

Each time you run the macro it should send a different keystroke.

Thanks

Hi @Pedro.Carmo,

It may be easily accomplished by introducing a control variable, named "VarName" in this example.
You need to set the initial value of this variable to either 0 or 1 and then disable/delete this action. You can change the name of the variable to a unique name so that its value is not changed by other macros unintentionally; it has to be a global variable in order for it to "remember" its last value and serve as a "switch" for future execution.

Switch Case.kmmacros (3.1 KB)

1 Like

Thank you!

@martin and @Pedro.Carmo, I'd suggest using a KM Variable name that is unique to this macro.
"VarName" is often the default Variable name proposed by KM. So this could lead to overwriting the variable you need for this macro.

I'd suggest a Variable name something like this: "DND_TKS__LastKeyUsed"
where:

  • "DND" means "Do Not Delete"
  • "TKS" is an acronym for "Toggle KeyStrokes", which would be my name for this macro

I use the double-underscore AFTER the acronym as a part of my standard variable naming convention.
This results in only the text to the right to show in a Prompt for User Input Action (not used in this Macro).

Also, you don't need to initialize the variable in this case:

Assuming that you want to type ⌘R first, we can test for "R" as the last key used. Initially the Variable will be undefined (or empty), so the "ELSE" ("otherwise") section of the IF/THEN/ELSE will be executed.

image

Make sense? If not, feel free to ask any follow-up questions.

Thanks, @JMichaelTX. I did suggest that @Pedro.Carmo use a unique variable name.
Your macro is better. It does not need to set an initial value. Even its value is changed, it will be reset.

1 Like

Thank you

1 Like

@JMichaelTX Need help again.

Please, i have a different situation now:

I need to alternate this between 3 different options...

1 Like

@Pedro.Carmo
Just use a Switch instead of an If.
Check Martin's macro, and add to it an additional Switch, in there, add an action that Sets the variable to, say, "S".

1 Like