I am using the following AutoHotkey script originally written by Laszlo to detect single, multiple, short and long presses of the Ctrl key:
Morse(timeout = 150) { ;
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
}
}
~Ctrl::
p := Morse()
If (p = "0")
MsgBox Short press
Else If (p = "1")
MsgBox Long press
Else If (p = "00")
MsgBox Two short presses
Else If (p = "01")
MsgBox Short+Long presses
Else
MsgBox Press pattern %p%
Return
The script works well to differentiate between Short and long Ctrl presses. However, I am facing an issue when using Ctrl key together with another keys like z, c, v, etc. In these cases, the script detects these combos as a long press on Ctrl key and dispalys the "Long press" message box. Instead, I want it to ignore any combination of Ctrl + other keys and allow the default system behavior.
Appreciate any help modifing the script
Here is the correct answer:
CHANGELOG:
I used the original Morse function written by
Laszlowhich detects single/multiple, short/long presses of any key. But it had some problems specially with modifier keys.With the great help of
Rohwedder, the script modified to detect key combinations (eg.Ctrl+z) or mouse movement (eg.Ctrl+Mouse drag) and let them to perform their default functionality.At the next step, I improved the script to reach a better performance.
Finally, I solved the conflicts of the
Altkey with disabling "Alt key acceleration", as well as theWinkey conflicts with the start menu.USAGE:
For different hotkeys, you can set different timeouts, as well as different pattern sequences.
For example, in a real case, I needed a single Shift key press (Pattern: 0) to send
Alt + Shiftto change the input language. But I noticed a lag when switching languages while typing very fast. So, I decided to call the Morse function with these parameters:Pattern := Morse(400, 1).This removed the lag, since it goes through the loop just once. It also fixed my other problem which conflicting short and long Shift key presses, as the timeout to detect the long press increased. The downside is that there are only two patterns to use: short and long presses !
REFERENCES:
Original Morse function by Laszlo
Rohwedder code