Else If

After triggering macro, I want it to "branch" into 2 different conditions...
ie A n B conditions

just like IF and ELSE IF

not nested IF

Problem is, theres no ELSE IF in Keyboard Maestro...

What are some workarounds?

So I could execute Condition A and Condition B separately after pressing a certain Key on the keyboard to trigger it...

Macro > branch > execute condition A or execute condition B

Is this possible?!

1 Like

The concept of ELSE IF does exist in Keyboard Maestro, even though there isn't a specific action with that name.

In your If Then Else action, nest another If Then Else within the "otherwise execute" (Else) block.

This is functionally (and semantically) equivalent to ELSE IF.

That's the wrong way round -- if you have only two branches you need an "If.. Then... Else...":

if testOne is passed
then
   do thing A
else
   do thing B
end if

(Note that while KM calls this action "If Then Else", the "Else" block is headed "otherwise execute...".)

Whereas an "Else If" would be like:

if testOne is passed
then
   do thing A
else if testTwo is passed
   do thing B
else
   do thing C
end if

Yes but for it to work, I have to press A or B at the same time with macro hotkey trigger (Key Pad .)

Did insert Pause Until before the IF block, but cant make it work...

Did you try this?
Pause Until.kmactions (891 Bytes)

Also, instead of nesting the If Then Actions you can put them one after the other. This can be good for testing for more possibilities than just two i.e. A, B, C instead of A, B.

EXAMPLE Test for keys A, B or C.kmmacros (5.5 KB)

Click to Show Image of Macro

The Switch Case Action works like this. But the Switch Case Action can't test for keys down.

2 Likes

Your logic is (sort of) right, it's the event sequence that is the problem. As @Zabobon says, if you only want to react to A or B you can pause until either are pressed:

Test if A or B.kmmacros (4 KB)

Image

...although in that case you don't need the nested "If" action -- with only two choices it is a single "If Then Else":

Test if A or B simpler.kmmacros (3.2 KB)

Image

But it really does depend on what you are trying to do. For example, this one will react to any, or no, key being down when you release the trigger key:

Test if A or B complicated.kmmacros (4.1 KB)

Image

2 Likes

Also, this is probably pointing out the obvious but in a real-use case simply waiting for a key to be pressed without some kind of user prompt would not be very useful.

It is more likely that at some point in the Macro you will ask the user if they want to branch to A or B (or C or whatever). In which case you can branch the Macro depending on their response including canceling the Macro if that is the choice. The below will work whether you click the buttons or just type A or B.

The Prompt replaces the pause, as the Macro won't carry on until you respond.

And calling the buttons A/A B/B etc means they can either be clicked or the key indicated after / can be typed. The /. after Cancel means that hitting Escape will activate that choice.

EXAMPLE Prompt for keys A or B.kmmacros (3.7 KB)

3 Likes

fantastic, thank you once again KM community!

@Zabobon @Nige_S
both solutions worked

the aha moment for me was adding 2 conditions for Pause Until (any of the following are true)!
good reminder here!

thanks for clarifying the logic flow for IF overall also you guys!