Pass-through original keystroke if conditions not met?

Does anyone know how to pass-through a keystroke if the macro conditions are not met?

My case: I have a macro which is activated by either CMD+ENTER or CMD+R. Both are used extensively as defaults in other applications, so I restricted the macro to a specific IF condition. If the condition is false, I’d like to just “pass along” the keystroke to the system, as if Keyboard Maestro weren’t there.

I can’t find an obvious way to do this…

You could use the If Else action.
And then have the pass through in the else

Thanks Jimmy, I already have an IF ELSE action. I am asking what to put in the ELSE condition. As far as I am aware there is no pass-through action in KM. How would you make a pass-through that echoes either of the hotkey triggers?

FYI, I know the simulate keystroke action. However I need it (or something else) to echo the original keystroke rather than set it to a predefined one. Make sense?

Adding an image of the current flow. I'm interested to know about the ELSE condition.

The only good way to “pass through” a hot key is to not trigger it in the first place.

In this case, the solution is likely to use a Macro Group that is targeted at the application in question and thus the hot key will only be triggered in the desired application.

Once the hot key captures a keystroke you are on shaky ground as far as “passing it through”. You can try typing it, and Keyboard Maestro will try not to re-trigger the macro, but it can’t always avoid it. And even if it can, you can end up with keystrokes out of order.

1 Like

Thank you Peter. Makes sense! Didn’t even consider the possible re-triggering issue…

Peter - Can you go into more detail regarding when the re-triggering might be an issue?

The reason I ask is that I was wanting to do something similar. In Final Cut, the Escape key often doesn’t do what I think it should do. But I figured I couldn’t solve this with KM due to the re-triggering issue. But after reading this thread, I tried it, and it works for what I’m trying to do, at least for the first thing I wanted to fix. (Yay!)

Before I go on to try and correct all the places where the Escape isn’t used the way I want it to, I’d like to know what things might cause issues with re-triggering, so perhaps I can avoid them?

Thanks.

When Keyboard Maestro simulates a keystroke, it tries to disable matching hot key triggers. But because simulated keystrokes go into a queue and are processed at some point in the future, there is no real way to know when to re-enable the hot keys. So it becomes dependent on how fast your Mac and the target application are.

So if you do something like this:

  • Trigger on hot key X
  • If some condition
    • Do stuff
  • Else
    • Type Key: X

This will usually work, but often it will actually go around in circles a bunch of times first - triggering on X, typing the X, re-triggering on X, typing the X, etc until eventually the X gets through.

As such, it is much better if you can avoid this kind of thing, and as far as Keyboard Maestro is concerned, it is not well defined as to whether a typed keystroke will or will not trigger a hot key.

Better is to try to rework your macro like:

  • Trigger on hot key X
  • If some condition
    • Do stuff
  • Else
    • Do other stuff

For example, often it is a command key, and the “other stuff” can actually be an explicit Select Menu Item action.

1 Like

Thanks for the explanation. It helps me know what’s likely to work, and what’s not. And it also explains why things got strange when I tried to do something that you couldn’t possibly foresee. Yikes! But entertaining. :stuck_out_tongue:

I understand the technical limitation. Events in the OS X queue are not serialized the way human’s think about them. It’s too bad.I just sat down to ask about this and found in the “Similar Topics” popup.Just in case anyone is interested, here is what motivated this for me.

I am tired of clicking each application’s variation on “don’t save”. in Save dialogs —I much prefer a keystroke. Some applications do it in a way that I think was introduced a long time ago by Microsoft: ⌘D, for “don’t save”, “delete”, or “delete copy”. It seems to me that these days most Mac applications require a mouse click for this. I made a macro that looks for a button “Don’t Save”, “Delete”, or “Delete Copy” and clicks it if it finds one. In each of my groups that are active in just one specific application in which I want this behavior, I put a macro that calls that one, and bound it to ⌥⌘D.

There are three things wrong with this:

  1. The application might not be showing a Save dialog (in which case the macro is a no-op, which I guess is OK, as long as that keystroke doesn’t trigger something else in the application.

  2. The keystroke might block the application’s use of it when there is no Save dialog showing.

  3. It would be much easier to bind this keystroke more globally, so I don’t have to put it in a separate group for each application, especially if I had to create that group just for that one macro.

I can either check for the presence of such a button, or catch a failure, but in either case I would want to pass through the keystroke.

I’ve been trying to think of a way to activate this keybinding only when there is a Save box open, but that just doesn’t seem plausible.

Well, here's the basis for a horrible kludge:

You'd need to modify it to look for whatever buttons you might see.

The idea for passing the keystroke on is this: Disable this macro so when we re-issue the command-d, we don't accidentally re-trigger this macro. Then re-type the keystroke. After we think there's a good chance the keystroke went through, re-enable this macro.

Like I said, it's a huge kludge. But if you play with it enough, it might work.

But you're on your own, your mileage may vary, causes cancer in lab rats, etc.

Yeah, that’s pretty much what I had in mind. I wonder if the delay and re-enablement could be done asynchronously. In reality, unless there were a simple “pass through keystroke” action, the situations in which I’ve found a need so far aren’t worth doing anything complicated to deal with.

After I posted that, I started thinking. If this were something for me, here's what I'd do:

  1. I'd use a hotkey that isn't used in other apps. Eliminates all sorts of issues. On my extended keyboard, I usually use F13 through F19, perhaps with one more more modifier keys.

I'd code the macro like this (not tested)

Don't Save.kmmacros (4.9 KB)

The variable "dsButtonNames" will have the names of various "Don't Save" buttons in it. It starts out empty.

Whenever this macro is triggered and it doesn't find a button with one of the names in the list, it prompts for the name of the Don't Save button and adds it to the list.

Done.

Then use it as needed, and start adding button names.

This is a workaround without hardcoded keys. That is, it will pass-through the trigger key (whatever it was) by storing the key to a variable, temporarily disabling the macro and finally typing the stored key again via AppleScript.

For example, in the Finder you type ⌘K: If a file is selected (hence ‘Rename’ is active in the menu) it will rename that file. If no file is selected the normal action of the hotkey is executed (‘Connect to Server…’ in this example.)

You get the idea.

(The many different triggers were just for testing.)



Pass-through Workaround.kmmacros (6.2 KB)


Edited (2016-07-07): Better description.