One Hotkey Toggles Two Actions?

Hi forum,

I'm new to Keyboard Maestro. I'd like to toggle between two Desktops with one Hotkey. So if I press Key X for the first time, it should trigger shortcut A (to bring up Desktop 1), if I press Key X for the second time, it should trigger shortcut B (to bring up Desktop 2)... I don't find any "toggle" command, so how would I accomplish that?

Thanks for your help,

You can:

  1. branch on whether a variable contains 'true' or 'false', and
  2. toggle the value of that variable to its opposite after completing the action.

Toggle.kmmacros (20.7 KB)


Got it. Thanks a lot!

1 Like

General question.

Are the variables “persistent” ? That is, the variable state will stay in memory so, if I call the macro later, the variable will still be set (at least until I log out)?

The variables are maintained (as named strings) across and between sessions by the Keyboard Maestro engine.

To have a look at its current set of variables (and to edit their values, or delete them, if you like):

Keyboard Maestro > Preferences > Variables

But if the Engine stops (e.g., reboot), then they would be gone, correct?

No – they persist between sessions and after reboots.

( They’re stored as text in plist files, and reloaded when KM next starts )

New Information - I think I can use that. Thanks.


I'm also trying to use a Toggle macro to switch between desktops but I haven't been able to get it to work. Were you successful with these variable actions?

Show works where tell fails.

Shall we have a look at what you've got so far ?

This is what I have but nothing happens. I'm not too savvy with coding so please forgive me if I might have some basic errors.

Basically I want to use F15 to toggle desktops which are assigned to F13 (desktop 1) and F14 (Desktop 2)


OK. Testing works best with incremental ambition.

  • I would first try toggling something simple and direct (alternately playing two different sounds),
  • before stepping up towards anything as indirect (and involving slightly off-scale keystrokes) as this.

How are you binding those keystrokes to particular Spaces/Desktops ?

Does direct simulation of them in Keyboard Maestro (without the additional ambition of toggling) already succeed in switching in the way you want ?

Also, check that you have a structure like IF (test) THEN one action OTHERWISE alternative action

rather than an IF THEN (chain of different actions possibly cancelling each other out) structure.

It may be sensible to start with the simple macro I posted above, and then make incremental changes to it, one by one, testing as you go.

Toggle.kmmacros (5.9 KB)

Thanks for your answer. It finally worked! But I had to assign a different set of key commands to the System Preferences shortcut, because the Function keys for some reason were not reacting to any command in KM. Thanks!!

1 Like