29

I'd like to trigger an event in AutoHotkey when the user double "presses" the esc key. But let the escape keystroke go through to the app in focus if it's not a double press (say within the space of a second).

How would I go about doing this?

I've come up with this so far, but I can't work out how to check for the second escape key press:

~Esc::

    Input, TextEntry1, L1 T1
    endKey=%ErrorLevel%

    if( endKey != "Timeout" )
    {
        ; perform my double press operation
        WinMinimize, A
    }
return
Jake1164
  • 12,291
  • 6
  • 47
  • 64
Matthew Lock
  • 13,144
  • 12
  • 92
  • 130

3 Answers3

39

Found the answer in the AutoHotkey documentation!

; Example #4: Detects when a key has been double-pressed (similar to double-click).
; KeyWait is used to stop the keyboard's auto-repeat feature from creating an unwanted
; double-press when you hold down the RControl key to modify another key.  It does this by
; keeping the hotkey's thread running, which blocks the auto-repeats by relying upon
; #MaxThreadsPerHotkey being at its default setting of 1.
; Note: There is a more elaborate script to distinguish between single, double, and
; triple-presses at the bottom of the SetTimer page.

~RControl::
if (A_PriorHotkey <> "~RControl" or A_TimeSincePriorHotkey > 400)
{
    ; Too much time between presses, so this isn't a double-press.
    KeyWait, RControl
    return
}
MsgBox You double-pressed the right control key.
return

So for my case:

~Esc::
if (A_PriorHotkey <> "~Esc" or A_TimeSincePriorHotkey > 400)
{
    ; Too much time between presses, so this isn't a double-press.
    KeyWait, Esc
    return
}
WinMinimize, A
return
Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
Matthew Lock
  • 13,144
  • 12
  • 92
  • 130
3

With the script above, i found out that the button i wanted to detect was being forwared to the program (i.e. the "~" prefix).

This seems to do the trick for me (i wanted to detect a double "d" press)

d::
keywait,d
keywait,d,d t0.5 ; Increase the "t" value for a longer timeout.
if errorlevel
{
    ; pretend that nothing happened and forward the single "d"
    Send d
    return
}
; A double "d" has been detected, act accordingly.
Send {Del}
return

Source

user2599522
  • 3,005
  • 2
  • 23
  • 40
0
$Esc::
    KeyWait,Esc
    KeyWait,Esc,D T0.6 ; you can adjust timeout 0.6 here
    If ErrorLevel
send, {Esc} ; single press action
    else
        WinMinimize, A ; double press action
return
Name
  • 339
  • 2
  • 18