Long Press key Conflict Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
haomingchen1998
Posts: 162
Joined: 20 Feb 2023, 16:37

Long Press key Conflict

Post by haomingchen1998 » 04 Jun 2023, 19:42

I wrote a code that long press ESC to do one thing, and double press ESC key to do something else. It will also pop a msgbox if I want to cancel, I assigned single press ESC key to press "No" in the msgbox if msgbox exist. One problem is when I long press the ESC to trigger an action, it pops the msgbox and instantly close the msgbox. Because the msgbox also requires ESC press to cancel the action. I tried ~ and $ before ESC, but it made the long press function turn into single press to trigger instead. I know I can change the hotkey to cancel the msgbox, but I want to use ESC throughout this code. I'm out of idea, any help will be appreciated.

Code: Select all

      ; The ESC trigger from line below CONFLICTS with line 20
      	ESC::
           KeyWait, %A_ThisHotkey%, T1.5    ; Long Press and Hold, THIS LINE CONFLICTS WITH LINE 19.
      	     If ErrorLevel {
            MsgBox, 4, Confirm to "FORCE" Eject all drives?, Confirm to "FORCE" Eject all drives?, 1
            return
            Keywait, ESC
         }
         If (A_PriorHotkey = A_ThisHotkey and A_TimeSincePriorHotkey < 400) { ; Double Press Twice
                     MsgBox, 4, Confirm ending process?, Confirm ending process?, 1
                     return
            KeyWait, ESC
            }
         Else Send, {ESC}

         #IfWinExist Confirm ending process? ; To press NO in the msgbox, so I can cancel the action if needed.
            Esc::
            ControlSend, , {RIGHT}{Enter}, Confirm ending process?
         #IfWinExist

         #IfWinExist Confirm to "FORCE" Eject all drives? ; To press NO in the msgbox, so I can cancel the action if needed.
         ; The ESC trigger from line below CONFLICTS with line 1
            Esc::
            ControlSend, , {RIGHT}{Enter}, Confirm to "FORCE" Eject all drives?
         #IfWinExist

User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: Long Press key Conflict  Topic is solved

Post by mikeyww » 04 Jun 2023, 21:01

Use :arrow: Return to end a hotkey subroutine.

Code: Select all

#Requires AutoHotkey v1.1.33

Esc::
If !Ta
 SetTimer, Ta, -300
Ta++
Return
Ta:
Switch {
 Case GetKeyState("Esc", "P"): ; Held
  MsgBox 4, Confirm, Go?
  IfMsgBox Yes
   MsgBox Confirm
 Case Ta = 2:                  ; Double
  MsgBox 4, Proceed, Go?
  IfMsgBox Yes
   MsgBox Proceed
}
KeyWait Esc
Ta := "", e := False
Return
#If !e && WinActive("Confirm")
Esc Up::e := True
#If (e || WinActive("Proceed"))
Esc::Send n
#If

haomingchen1998
Posts: 162
Joined: 20 Feb 2023, 16:37

Re: Long Press key Conflict

Post by haomingchen1998 » 05 Jun 2023, 08:34

mikeyww wrote:
04 Jun 2023, 21:01
Use :arrow: Return to end a hotkey subroutine.

Code: Select all

#Requires AutoHotkey v1.1.33

Esc::
If !Ta
 SetTimer, Ta, -300
Ta++
Return
Ta:
Switch {
 Case GetKeyState("Esc", "P"): ; Held
  MsgBox 4, Confirm, Go?
  IfMsgBox Yes
   MsgBox Confirm
 Case Ta = 2:                  ; Double
  MsgBox 4, Proceed, Go?
  IfMsgBox Yes
   MsgBox Proceed
}
KeyWait Esc
Ta := "", e := False
Return
#If !e && WinActive("Confirm")
Esc Up::e := True
#If (e || WinActive("Proceed"))
Esc::Send n
#If
This code is so clean, and I just made it work. Thank you so much!

Post Reply

Return to “Ask for Help (v1)”