Macro Trigger Option Using Long Press of Hot Key [Example]

MACRO: Trigger Option Using Long Press of HotKey [Example]


VER: 1.0      2016-07-30 ~~~~~

Download:

Trigger Option Using Long Press of HotKey [Example].kmmacros (12 KB)


PURPOSE:

• Demonstrate how to make use a long press of the hot key trigger to invoke optional processing in your macro
• This NOT a finished macro, ready for use in a production environment
• It is a teaching example to show you how to use this concept in your own macros.

Author: @JMichaelTX, @Tom, @DanThomas, others
• While I wrote this specific macro, the original idea/example was
posted by @Tom, and then @DanThomas. (see references below)
• Others may have pioneered this idea

HOW TO USE:
(1) Normal Use: Quickly press and release the Hot Key
(2) Optional Use: Press and hold Hot Key trigger for at least 0.5 sec
• You should hear a brief "Tink" sound

Macro Setup

  • Change the Macro Hot Key Trigger to whatever you'd like
  • Then change the Action "CHECK FOR LONG PRESS" to use the same key and modifier key

REFERENCES:

• This macro: Macro Trigger Option Using Long Press of Hot Key [Example]
@Tom: Select All and Copy
@DanThomas: MACRO: Run Current Macro



Example Results

7 Likes

Some more examples:

4 Likes

This is pretty slick, thanks for posting!

There is a tiny mistake: The first IF statement condition is “All of the following are true”

It not really a mistake, just a design choice.
As it, it will be true if EITHER the main key OR the modifier key are down. This just makes it easier to use.

Oh~ That makes sense.

Hello,

As many of you probably noticed the script had an issue when e.g. you pressed quickly the "Trigger" multiple times after one another, e.g.:

Short Press -> Release -> Long Press (Hold) (all within the 0.5 second pause period) would recognize only one long press. Preferred would be that it recognized one short press and then one long press.

Happily Keyboard Maestro introduced in Version 9 a new option being able to access the unique ID of a script instance (https://wiki.keyboardmaestro.com/token/ExecutingInstance) which can be used in order to prevent the above described issue.

Please find here a modified "Version 2" of above script:

Trigger Option Using Long Press of HotKey v2.0 [Example].kmmacros (21.2 KB)

0%20%5BExample%5D

3 Likes

Sorry to bump this thread, but wanted to ask:

Can this macro be called using the Execute Macro action?
To keep it as one macro and avoid duplicating it on every macro where I'd like to use a Long/Short press, would like to know if this macro can be used in a Execute Macro action, and somehow use with long and short key press detection.

Of course, since any macro can be called with that.
But it would not be effective.

I don't think that would be very efficient, since the "long press" must be detected with the user presses the hot key that triggers the main macro.

The other issue is that the actions in this macro must be adjusted for the specific hot key being used.

What I have done is to put all of the "long-press" detection in a KM Group Action, and save that to my KMFAM items. Then I just insert from KMFAM when needed.

Here's my setup:

image

2 Likes

thanks, I have the KMFAM macros but haven't used them so far,
this will help start using them, appreciated!

Oh geez, you REALLY need to use KMFAM. Call it a gift.

1 Like

Thanks!
Tested with a Comment with a Green background, and saw it remembers even the background as well,
and noticed how valuable it is... don't know why I didn't try it sooner :facepalm:

I finally got around to trying out KM 10's new "long press" trigger, but was then a bit disappointed to learn that it's actually implemented as "long press and then release", which means you either have to keep the key pressed longer than necessary or you risk pressing it too short, because you will only know after you have released the key whether it was long enough or not.

The approach presented in this thread (and others) avoids this awkward uncertainty, but it suffers from another downside: Executing the actions after a short press is delayed by the "Pause" action, which makes them feel a bit laggy. This is not much of an issue for less frequently used macros, but I wanted to overload some of the hotkeys that I frequently use and for those, delaying the short press actions by 300-500ms was not an option.

It then occurred to me that I could reverse the logic for detecting short vs. long presses: Instead of waiting some time and then checking whether the key is still pressed, I could repeatedly check whether the key is no longer pressed and execute the short press actions as soon as the key is up:

This macro checks (roughly) every 50ms whether the key is up. If it is, the short press action is executed immediately and the macro is cancelled. If after (roughly) 8 x 50ms = 400ms the key is still not up, the long press action is executed.

I don't know if there is something that I'm missing that would make this approach not a good idea, but it seems to work really well for me.

1 Like

Brilliant!

I've managed to incorporate it into my Single/Double Tap & Long Press macro. It's slightly snappier than my previous method of detecting double presses, but has one drawback that may or may not bother you. Two single taps, which occur outside of the double tap window, but during the period the macro is listening for a long press, will be seen as a long press. In other words, if you want to do single taps in relatively quick succession, this ain't the macro for you. Perhaps there's a way to improve it, but I can't think of one.

Edit: I fixed the above issue, added the ability to choose what type of presses it will detect for, and added a triple-tap option because... why not?

Multipress.kmmacros (53.7 KB)

The green actions will require attention when setting up:

Screenshot

I kept seeing the word "multipress" in your macro. My mind was repeating the word "multipress, multipress, multipress." Suddenly my brain remembered a video clip. Go to Youtube and search for "LEELOO DALLAS MULTIPASS". (It's just 23 seconds in length.)

1 Like