Simulating a Function Key in KM Not Triggering the Macro with the Function Key as Trigger


I have a set of 9 macros that use F1-F9 as triggers.

When I press the physical Function keys on my keyboard, the macros will fire.

I want to explore an alternative method, using a variable Var_Num4Func to control which Function key to simulate.
Currently, its value is 7. With the macro shown below, it is supposed to simulate F7. I executed the macro, but the macro that is supposed to be triggered by pressing F7 is not executed.

I added the debug action group, to display the value of the variable Var_Num4Func. The action is successfully executed, displaying the value of the variable. I assume, then, F7 was successfully executed.

My question is: Does this mean that simulating a keystroke will fire the macro with that keystroke as trigger? Or did I do something wrong?


you can use %TriggerValue% directly to pass "F+number"

Example just using F7

you can add more to the same macro as a trigger (ie F8, F9, etc) and to the switch as well.


More complete example:

Hi @hello,

Thanks for your reply. But I'm not understanding whether your example solved my problem.

What I was trying to accomplish is this:

Let say, the initial value for the variable Var_Num4Func is 1.

When I execute this macro (Macro #1), since the value is 1, KM will simulate F1 press.
I hope the F1 then will trigger another macro (Macro #2) that has F1 press as trigger. → What I can tell is: it looks like the F1 keystroke is simulated, but but Macro #2 is not executed. This is my problem.
Then, Macro #1 will continue macro, which will set Var_Num4Func to Var_Num4Func + 1, turning its value to 2.

Then, when I execute Macro #1 again, since now, Var_Num4Func = 2. It will trigger the simulating F2 keystroke action, which is supposed to trigger Macro #3, which has F2 as trigger.
Then, Macro #1 will continue macro, which will set Var_Num4Func to Var_Num4Func + 1, turning its value to 3.

And, when I execute Macro #1 again, it will simulate F3 keystroke, and trigger Macro #4 which has F3 as trigger. And so on.

In other words, I used to need F1-F9 to trigger 9 macros. But with this new setting, I need only two macros, one to loop through F1-F9 keystroke simulation by pressing 9 times, the other macro to reset the value of Var_Num4Func to its initial value 1.

You can instead of Simulate Keystroke, use the Execute Macro action

Thanks for the suggestion. However, I'm using the Simulate Keystroke for a reason. I have multiple sets of macros that use the function keys as trigger, but I only activate one set at a time. I should have mentioned this.

I'm more curious about why Simulating Keystroke does not trigger the macros that uses the keypress as trigger? :joy:

from the wiki:[]=execute

:warning: Note that whether or not a key typed by Keyboard Maestro itself will trigger a hot key (and thus be swallowed and execute a macro) or not (and thus go through to the application is not defined - either behaviour may happen and which behaviour happens will vary depending on many unpredictable factors.

Thanks, @Joseki.
If I press the hotkey and it is also swallowed, then it makes more sense. But now, when I press the physical key, the macro is triggered, but simulating the hotkey press does not trigger the macro. I'm puzzled...

KM can not simulate physical keys

So you mean simulating keystrokes cannot trigger macros that use those keys as trigger?

It's simple to test this.

Here's my triggering macro:
KM 0 2021-07-10_00-03-20

Here's my triggered macro:
KM 1 2021-07-10_00-03-50

Running macro Test Begin does not trigger macro Test Triggered by Begin.

However, pressing F3 does trigger macro Test Triggered by Begin.

So it would appear that when KM simulates the pressing of a function key, it is not the same as actually pressing the function key with your finger.

1 Like

Hi @tiffle,
Thanks for taking time to test for me. I should have done it myself. I can confirm that "Simulate keystroke" can not be used as keypress/down/release/tap triggers.

Hi @peternlewis, can I make a feature request to allow KM to use "Simulate keystroke" to trigger macros as "key press" will do? This will be useful in my use case.

No. Use the Trigger Macros by Hot Key action for that.


I did not know there is already a dedicated action for that purpose!
Thank you so much!!!

1 Like