How to wait for any key in a macro

I want to mimic display sleep via a macro which fiddles with the screen brightness (because display sleep make a beloved app I'm running crash).

So I thought of the following steps:

a) Storing current screen brightness
b) Lowering the brightness
c) Waiting for keystroke
d) Restoring normal brightness

I have two problems here:

  1. How to read out the current brightness . (I couldn't find anything in the manual when searching for "brightness variable")

  2. How to wait for any key press. (I tried an Execute shell script action with the read built-in, but this action always cancels, even if I switch off action timeout.)
    3write a macro which lowers the screen bridtness then waits until sombody

Any ideas about this?

Does anybody know why the Execute shell script action cancels out when using read in the script? And what could be an alternative?

I don't see a way to test for ANY key, but you can test for one or more keys using the Pause Until action, and include the most common keys you might use:


Cool! Tried it - and the first part (The key ... is down) works, as does the These modifiers are pressed part.

@JMichaelTX, what is the The typed string was otherwise condition for? - For me that condition always terminates the pausing and lets the whole macro run from the beginning to the end.

Also, more importantly, how can I "eat" the character the Pause Until.. action detects but not uses? (If I don't eat it it will passed to the active app.)

ps: I attach the lengthy Pause Until Key Pressed action for other people to use. - It should contain most keys...

Pause Until Key Pressed.kmactions (11.7 KB)

1 Like

Sorry, that was left in due to error -- I was testing it.

Good question -- I don't know.
Maybe @peternlewis can give us some ideas.

You cannot, not this way.

In order to β€œeat” the characters you have two options:

  • Use something like an Alert action to display an alert (or any other Keyboard Maestro window that accepts input) so that your input goes there. But, of course, that means all input goes there, which is probably not what you want in this case.
  • Enable or activate a hot key triggered macro with the specific keys. This is generally a better way to detect keystrokes anyway, since otherwise tapping the key could be missed, especially if one part of the condition is slow.

Instead of @JMichaelTX’s pause, you do:

  • Set variable "Continue" to 0
  • Enable Macro "Watch For Continue"
  • Pause Until variable "Continue" is not 0

Macro "Watch For Continue"

  • Hot Key: Escape
  • Hot Key: Space
  • USB Device Key: Shift Key
  • Disable Macro "Watch For Continue"
  • Set variable "Continue" to 1
1 Like

Makes sense. I will try it when I'm back at my Mac. Thanks a lot, @peternlewis!

Also, is there a way to retrieve the current screen brightness and store it in a variable?

Hi @halloleo, for a new question it is suggested that you make a new post with that question. This makes it possible for others to search and benefit from the clear posting title.

1 Like

I was just reading through this thread again, and it struck me: Why do you need to pause for "any key"?

I have a number of macros that need to pause until the user finishes taking some actions outside of the macro. So, I generally use a "Pause Until" the user presses OPT-RETURN. Of course you could use any keystroke that you would not be using during the pause period. This has always worked very well for me -- simple and effective.

Good point @troy! Here you go.

@JMichaelTX I just would like to replica/simulate the behaviour of Sleep Display: After sleeping the display it wakes on any key.

Just learned the enable/disable macro technique.
Thanks Maestro.

@peternlewis Thanks for your really interesing approach with a "Watch To Continue" macro triggered by the keys on my keyboard! ! I tried it out - and it somewhat works. :slight_smile:

However I ran into the (once discovered obvious) issue that some keys (function keys in my particular case) already have triggers, so KM displays a palette instead of executing the "Watch For Continue" macro - and doing so while the screen is darkened and nobody can see it...

The only easy way around which I found is: I don't add the function keys to the trigger for "Watch For Continue". Any other idea? Is there a way to say this trigger has priority over other triggers?

No. All triggers are equal - triggers are either active or not. The only way to avoid this is to ensure there are no other matching macro/triggers active at the time.

Thanks. Will do without the function keys.

Also, if anybody is interested in my "Watch For Continue" macro with the many keys as trigger, I attach it here:

Sleep Screen by Darken - Watch To Continue.kmmacros (24.1 KB)