Short Press Shortcut Do One Action, Long Press Do Something Else

Continuing the discussion from Select All and Copy:

I am trying to think of a way that you could make it so if you long press a keyboard shortcut it only does the second half of a macro (or the first half that part doesn’t really matter too much). This way you get two commands out of one based on time held down and you don’t have to mess with the conflict pallet and a second keystroke.

I am trying to make a macro that pastes in a value of " - 01" and then short press it again and it increments to then next number " - 02" etc indefinitely (or at least to 99). Then if you long press the same shortcut it resets back to 01 again and resumes pasting in the next number upon short presses.

2 Likes

Something like this?

Paste Counter.kmmacros (3.9 KB)

4 Likes

Exactly like that Dan, all I can say is wow!! Thanks for lending me your brain for a little bit :smile:

Great!

I'm not using it much, so why not? :wink:

For a more general solution, readers can see:

Great thanks, I will check that out.

Dan you help me understand this ^\d\d?$

It is somehow saying if the key command does not match less then the delay time then start "MyCounter" back at 1.

With "any of the following are true" then with the long press it resets the value (as desired) and under the pause value will increment to the next value number.
If you change the if to "none of the following are true" then it reverses the behavior.
If you change the if to "not all of the following are true" then it always resets the value.
If you change the if to "all of the following are true" then it always counts up and never resets the value.

I'm trying to wrap my head around this but am a bit lost at how it is working though I have been using it a lot!

I can get my head around @JMichaelTX reference post but it doesn't seem to be as compact as what you have done as far as I can tell.

https://wiki.keyboardmaestro.com/manual/Calculations

^ = power operator
\ = I have no idea
D = I don't know date and current time
? = ternary operator (whatever that means)
$ = Variables or array access (okay new to me, use to that being fixed values in Excel)

I have given it a valent effort to try to make sense of it and you just whipped this out in about 10 minutes or less after my initial post!

that macro is really nice - very nice, thank you.
I really wish there was a way to do that with the
tapped once, twice, three times trigger type.
tapped once = macro A
tapped twice = macro B
tapped three = macro C

As you know, the way it is currently programmed, if you tap the trigger three times it also triggers the twice and once keys - not so useful as I can see (although I’m sure there’s a use) I just think the example I laid out would be SO much more useful.
Just my thought.
cheers

@skillet, please use whatever solution works best for you.

But I'm not sure what you mean by my macro "doesn't seem to be as compact as what you [@DanThomas] have done . . . ".

Dan's macro has about 7 Actions, whereas mine has about 8 (not counting Comment Actions). So it would seem to me that they are about the same "compactness". :wink: But I am always looking to improve my stuff, so please feel free to make any suggestions to do so.

Keep in mind that my macro is designed as an example for users, so it favors clarity over compactness. After you download it, you can optimize it however you like.

Having said all that, I think all you need to do to use my macro is:

  1. Change the Hot Key to your preference
  2. Change the keys used in the "Check for Long Press of Trigger" Action to match the Hot Key Trigger you use:
    .

    .
  3. Insert the Actions you want for a long keep press, and or normal key press, in the "Process Actions Based on Long Press Option" Action:
    .

You can replace my "Display Text" Actions with your Actions, if you like.

Does that make sense?

That's a Regular Expression. It boils down to this:

^    = Start of the variable
\d   = Single numeric digit (0-9)
\d?  = Single numeric digit, optional (that's what the question mark means).
$    = End of the variable

Keyboard Maestro “Trigger Option Using Long Press of HotKey [Example] - Increase Number by One” Macro

Trigger Option Using Long Press of HotKey [Example] - Increase Number.kmmacros (14.5 KB)

You're right it isn't much longer in the benefit of yours is I could follow it I counted 11 needed actions for yours and 7 for Dan's. 10 if you don't use the last action to clear it out to be safe and then you would need the action to set the variable if you don't have the keys pressed down, so I guess you could remove one or the other leaving you 10 (I didn't count the Display Text action I left in there). Your macro you had to set a variable and then use that variable's result to then choose to do the next actions where Dan's doesn't seem to need an extra variable to then do the variables he has in his macro. Some sort of magic voodoo is being done with ^\d\d?$

Also I noticed that I can keep the modifier held down with Dan's when one is set and release the action key (i.e. Command+F4 I can still hold down command and release F4) and it will then do the short press. I realized that in order to get that same behavior I couldn't just not select any modifiers I actually had to remove the modifier option. Otherwise I have to release the modifier and the action key to get it to work which is not as natural to me. If I take off the option for the modifier then it never does the short press action, a bit confusing but good to know.

Thank you for your reply and help.


Short Press Shortcut Do One Action, Long Press Do Something Else - Questions & Suggestions - Keyboard Maestro Discourse

2021-11-08 02:05:44


If you want simple and compressed, here it is.

BTW, your macro failed if the first execution was a normal press because the "MyCounter" variable did not exist.

A long press of the trigger, ⌘1, will reset the counter to zero.

MACRO:   Increment Number [JMichaelTX]

--- VER: 1.0    2016-10-14 ---

DOWNLOAD:

Increment Number [JMichaelTX].kmmacros (4.2 KB)


image

@skillet, @DanThomas answered your question.
For future reference, here's a great site to test and understand RegEx:

For example, it will show you this:

@skillet:

Here's a minor update you may want to make to cover the case where MyCounter exists, but is not numeric:

As soon as Apple adds a prescience API, so I can tell in advance whether you are going to press it a second or third time or not, I will be happy to implement it this way.

You can do what you want in a variety of ways, but all involve adding a delay to the first and second macro to see if another trigger will fire.

For example:

First
Trigger: tapped once
Action:
Set Do First to 1
Pause
If variable Do First is 1 then
Macro A

Second
Trigger: tapped twice
Action:
Set Do First to 0
Set Do Second to 1
Pause
If variable Do Second is 1 then
Macro B

Second
Trigger: tapped thrice
Action:
Set Do Second to 0
Set Do Third to 1
Macro C

Oooh, I want that too! But then again, if they had that technology, they'd already know I want it...

:wink:

1 Like

LOL LOL, yeah the word thing is funny Dan, - =)
Peter, thank you for your reply and your other replies to my ‘beginner’ posts.
Since ‘coming over’ from quickeys, I really want to tell you that I am SO enjoying being a part of this community AND having such strong, flexible, supported software to work with.
~ Can’t thank you enough or say enough positive things about KM. -
nice

Here's a template for creating macros that allow multi-press hotkeys:

WOOF! just awesome Dan, - flexible, quick, .3 seems perfect timing for the delay, but can adjust, nice, THANK YOU-

1 Like

Oh wow thank you, I can wrap my head around that, very efficient and compact.

Very nice thanks for sharing, https://www.regex101.com is completely new to me and seems a bit daunting, I'll have to read up on it http://www.regular-expressions.info

Good point I didn't think of that since I had already run it before several times. [quote="JMichaelTX, post:14, topic:4855"]

Here's a minor update you may want to make to cover the case where MyCounter exists, but is not numeric:
[/quote]

Hum, looks like I'll be learning about Regular Expressions in the future.

Here is one slight modification if anyone might come across this and find it useful to be able to type in your own number with a long press that I added and have been using from @DanThomas macro, I added to what @JMichaelTX just posted.
Increment Number [JMichaelTX].kmmacros (4.2 KB)

1 Like