It took a lot of reading and learning along the way and uses a couple techniques I don't see much on the forums.
- Variadic Functions: used to send in as many press handlers as desired.
- Dynamically Calling Functions: used to fire the correct handler.
Without further ado... here is...
HandleMultiplePresses()
Code: Select all
HandleMultiplePresses(pressHandlerNames*) {
Recurse:
keyPresses += 1
strippedHotkey := StripHotkeyModifiers(A_ThisHotkey)
KeyWait, %strippedHotkey% ; Wait for KeyUp.
KeyWait, %strippedHotkey%, D T.12 ; Wait for same KeyDown or .12 seconds to elapse.
keyPressedBeforeTimeout := (ErrorLevel = 0)
If (keyPressedBeforeTimeout) {
Goto, Recurse
}
Return pressHandlerNames[keyPresses]()
}
StripHotkeyModifiers(hotkeyToStrip) {
; from https://autohotkey.com/board/topic/32973-func-waitthishotkey/
RegExMatch(hotkeyToStrip, "i)(?:[~#!<>\*\+\^\$]*([^ ]+)( UP)?)$", Key)
Return Key1, ErrorLevel := (Key2 ? "Down" : "Up")
}
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
F1:: HandleMultiplePresses("SayGo", "SayFight", "SayWin")
^F1:: HandleMultiplePresses("SayGo", "SayFight", "SayWin")
F2:: HandleMultiplePresses("SayWin", "SayGo")
SayGo() {
MsgBox, %A_ThisHotkey% says Go!
}
SayFight() {
MsgBox, %A_ThisHotkey% says Fight!
}
SayWin() {
MsgBox, %A_ThisHotkey% says Win!
}
I'll keep the post up to date with any improvements. Feedback welcome!