How can I avoid sending the keys used to trigger a macro?

I have a macro that runs a bit of AppleScript. That script contains code to simulate pressing an arrow key during execution. Here is the problem: when AppleScript runs the script, it “sees” the modifier keys that I used as part of the trigger for the KM macro, and this changes the key code that the AppleScript ultimately sends to the application. (In my specific case, I trigger the macro using control-⌘-k, the key press simulated by the script is ⌘-➜, but the application ends up getting control-⌘➜)

Is there a way to deliberately “clear” the keys being held prior to the execution of the KM macro actions? Basically, I need a step before the execution of the AppleScript that says “pretend nothing is being pressed right now”.

It must be something else causing the behavior. I just ran this simple KM Macro that worked perfectly:

I rarely use the AppleScript key code and keystroke commands now that I use KM. If I need a keystroke, I use the KM Type a Keystroke Action. But I only use that IF there is not an app menu item for the action. I prefer to use the KM Select or Show a Menu Item action (KM Wiki).

If you still need help, it would be best if you post your macro and script.

As @JMichaelTX says, AppleScript is presumably simulating the right key strokes, but it is possible your target application is looking at the keyboard modifiers directly, rather than the keystroke events (this would generally be bad behaviour, which you would expect only from a cross platform/Java/etc application of some sort).

The best solution at that point is to start your macro with a Pause Until action, with the Modifiers condition, set to nothing pressed, so your macro waits until you release the Command key and then continues with the simulated keystrokes.

:smile:

IMO, the best solution is to NOT use AppleScript for the keystroke. Instead, in order of preference, use one of these KM Actions:

  1. Select Menu
  2. Type a Keystroke.

unless there is some overriding reason to use AppleScript.

That may well be true. There isn’t normally a menu for arrow keys though, and if the app is ignoring the modifier-less keystroke from AppleScript, it may well ignore it from Keyboard Maestro’s Type a Keystroke as well.

Perhaps, but I can tell you that since I've been using KM I almost never use AppleScript for keystrokes. And, I've never had the issue of the trigger of the Macro interfering with KM Type Keystroke. I've done some very complicated KM Keystrokes, in some difficult apps, and I find it is far easier to use KM keystrokes, to change them, to change sequence, to add pauses, etc.

Just my 2¢. :wink: