Handling single, double, triple, quadruple... infinite key presses.

Post your working scripts, libraries and tools
User avatar
GollyJer
Posts: 38
Joined: 19 Sep 2015, 19:33
GitHub: GollyJer

Handling single, double, triple, quadruple... infinite key presses.

18 Nov 2017, 13:43

I wanted an easy way to handle multiple presses of a hotkey. As far as I could Google there weren't any good solutions, so I dug in created this one. :P

It took a lot of reading and learning along the way and uses a couple techniques I don't see much on the forums.
It also relies on the ErrorLevel returned from KeyWait, which is useful to understand.

Without further ado... here is...
HandleMultiplePresses()

Code: [Select all] [Expand] [Download] (Untitled.ahk)GeSHi © Codebox Plus



How do you use it?
After a hotkey, call HandleMultiplePresses with the number of function names matched to the number of key press counts you want to handle. The order defines the handler.
2 presses? Handled by the second parameter. 3? The third. etc.
An example will help.

Code: [Select all] [Expand] [Download] (Untitled.ahk)GeSHi © Codebox Plus



I'm certain I'm missing something and this can be improved. But, it works pretty great.
I'll keep the post up to date with any improvements. Feedback welcome!
Last edited by GollyJer on 27 Nov 2017, 20:53, edited 2 times in total.
Helgef
Posts: 2476
Joined: 17 Jul 2016, 01:02
Contact:

Re: Handling single, double, triple, quadruple... infinite key presses.

18 Nov 2017, 14:53

Very nice, well done :thumbup: .
Suggestion, on the last press, call the function without waiting, eg

Code: [Select all] [Download] GeSHi © Codebox Plus

keyPresses += 1
if (keyPresses == pressHandlerNames.length())
return pressHandlerNames[keyPresses]()

You might aswell use return, if the function returns something.

Cheers, thanks for sharing.
User avatar
GollyJer
Posts: 38
Joined: 19 Sep 2015, 19:33
GitHub: GollyJer

Re: Handling single, double, triple, quadruple... infinite key presses.

18 Nov 2017, 15:17

Very nice, well done :thumbup: .

Hi Helgef... thanks.

Suggestion, on the last press, call the function without waiting?

I would consider this unexpected behavior. What if some game needs me to press the key 20 times but I only want to do something special up to 3 times?

You might aswell use return, if the function returns something.

I don't see how it can hurt... code updated.

Cheers, thanks for sharing.

You're welcome!
rommmcek
Posts: 297
Joined: 15 Aug 2014, 15:18

Re: Handling single, double, triple, quadruple... infinite key presses.

21 Nov 2017, 21:17

Exorbitant! Specially I like this behavior:

Code: [Select all] [Download] GeSHi © Codebox Plus

$F1:: HandleMultiplePresses("F1", "SayFight", "SayWin", "", "", "", "", "SayGo")
F1(){
Send, {F1}
}
preserving native key response and not throwing an error for empty function "" !
I modified to:

Code: [Select all] [Download] GeSHi © Codebox Plus

KeyWait, %strippedHotkey%, D T.3
Works fine for me, but have you any concerns about it?

Thanks!
User avatar
GollyJer
Posts: 38
Joined: 19 Sep 2015, 19:33
GitHub: GollyJer

Re: Handling single, double, triple, quadruple... infinite key presses.

22 Nov 2017, 15:58

Exorbitant! Specially I like this behavior:

Code: [Select all] [Download] GeSHi © Codebox Plus

$F1:: HandleMultiplePresses("F1", "SayFight", "SayWin", "", "", "", "", "SayGo")
F1(){
Send, {F1}
}
preserving native key response and not throwing an error for empty function "" !

Happy to know you like it!
And yes. That behavior is exactly what I was going for... the flexibility to handle (or not) any press sequence you desire.

I modified to:

Code: [Select all] [Download] GeSHi © Codebox Plus

KeyWait, %strippedHotkey%, D T.3
Works fine for me, but have you any concerns about it?

No concerns. I chose a wait time that didn't feel long after a single press and I could achieve multiple presses without making a mistake.
fenchai
Posts: 103
Joined: 28 Mar 2016, 07:57

Re: Handling single, double, triple, quadruple... infinite key presses.

22 Nov 2017, 17:51

Same as RapidHotkey() ?
I think RapidHotkey even allows sending texts, subs, actions.

but still pretty awesome work
User avatar
GollyJer
Posts: 38
Joined: 19 Sep 2015, 19:33
GitHub: GollyJer

Re: Handling single, double, triple, quadruple... infinite key presses.

27 Nov 2017, 20:51

fenchai wrote:Same as RapidHotkey() ?
I think RapidHotkey even allows sending texts, subs, actions.

but still pretty awesome work

HandleMultiplePresses allows you to define ANY function. So it can send texts, subs, actions... or am I missing what you mean?

It seems RapidHotkey does the same thing but is significantly more complicated. Also it doesn't, as far as I can tell, allow for different functions to be executed depending on the press count.
For those interested RapidHotkey is here.
fenchai
Posts: 103
Joined: 28 Mar 2016, 07:57

Re: Handling single, double, triple, quadruple... infinite key presses.

28 Nov 2017, 13:48

GollyJer wrote:
fenchai wrote:Same as RapidHotkey() ?
I think RapidHotkey even allows sending texts, subs, actions.

but still pretty awesome work

HandleMultiplePresses allows you to define ANY function. So it can send texts, subs, actions... or am I missing what you mean?

It seems RapidHotkey does the same thing but is significantly more complicated. Also it doesn't, as far as I can tell, allow for different functions to be executed depending on the press count.
For those interested RapidHotkey is here.


I think RapidHotkey is more complicated but allows more customization while yours is simpler but has no way to change the timing of how long is double press and so on.

But I still would use yours for simplicity.

Return to “Scripts and Functions”

Who is online

Users browsing this forum: No registered users and 14 guests