With different hotkey press patterns many more actions can be triggered than just with a hotkey press itself. For example, the key "a" sends "a", two short presses sends "ä", a long press sends "á", and so on. Like Morse codes for different actions.
The following Morse function determines the base key from the current hotkey, and starts monitoring it. If it is released after longer time than timeout (default to 400ms), "1" is appended to the Pattern variable. If the key is released sooner, "0" is appended.
Then the scripts waits for the key to be pressed down again. If it is not pressed within the timeout period, the 0-1 pattern of the last key presses are returned, otherwise the next key hold time will be appended to the Pattern. Usage examples are at the end of the script, for Alt-Z and BackSpace.
Code:
Morse(timeout = 400) { ;
tout := timeout/1000
key := RegExReplace(A_ThisHotKey,"[\*\~\$\#\+\!\^]")
Loop {
t := A_TickCount
KeyWait %key%
Pattern .= A_TickCount-t > timeout
KeyWait %key%,DT%tout%
If (ErrorLevel)
Return Pattern
}
}
BS::MsgBox % "Morse press pattern " Morse()
!z::
p := Morse()
If (p = "0")
MsgBox Short press
Else If (p = "00")
MsgBox Two short presses
Else If (p = "01")
MsgBox Short+Long press
Else
MsgBox Press pattern %p%
Return
Edit 20070224: Fixed key hold time, not to include key-up time