Simple Double key tap and long press for same key

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
scarii
Posts: 2
Joined: 09 Jun 2023, 16:05

Simple Double key tap and long press for same key

Post by scarii » 09 Jun 2023, 16:29

hi, I'm new with this so please help me with an easy to understand reply cuz i have 0 programming experience.
Need an script to these
1. i double tap "A" key to obtain a hotkey "+a" [shift+a if I'm right]
2. i hold "A" key to obtain a hotkey "+a"
3. i hold "+b" key to obtain a hotkey "a" key

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

Re: Simple Double key tap and long press for same key

Post by mikeyww » 09 Jun 2023, 21:29

Welcome to this AutoHotkey forum!

An example is below.

Code: Select all

#Requires AutoHotkey v2.0
$a:: { 
 Static presses := 0, squelch := 300
 hk := StrReplace(ThisHotkey, "$")    ; Strip the dollar sign
 If KeyWait(hk, 'T.5')                ; If key was quickly released,
  SetTimer(done, -squelch), presses++ ;  then reset timer, and increment counter
 Else Send('+' hk), presses := -1     ; If held, send the shifted key
 done() {                             ; Key presses have ended
  Switch presses {
   Case 1: Send hk                   ; Single
   Case 2: Send '+' hk               ; Double
  }
  presses := 0
 }
}

User avatar
scarii
Posts: 2
Joined: 09 Jun 2023, 16:05

Re: Simple Double key tap and long press for same key

Post by scarii » 10 Jun 2023, 22:40

mikeyww wrote:
09 Jun 2023, 21:29
Welcome to this AutoHotkey forum!

An example is below.

Code: Select all

#Requires AutoHotkey v2.0
$a:: { 
 Static presses := 0, squelch := 300
 hk := StrReplace(ThisHotkey, "$")    ; Strip the dollar sign
 If KeyWait(hk, 'T.5')                ; If key was quickly released,
  SetTimer(done, -squelch), presses++ ;  then reset timer, and increment counter
 Else Send('+' hk), presses := -1     ; If held, send the shifted key
 done() {                             ; Key presses have ended
  Switch presses {
   Case 1: Send hk                   ; Single
   Case 2: Send '+' hk               ; Double
  }
  presses := 0
 }
}
Thank you very much sir, really helpful

Mskrad
Posts: 3
Joined: 20 Dec 2023, 17:03

Re: Simple Double key tap and long press for same key

Post by Mskrad » 06 Jan 2024, 11:59

Thank you so much for this simple but elegant code!

Mskrad
Posts: 3
Joined: 20 Dec 2023, 17:03

Re: Simple Double key tap and long press for same key

Post by Mskrad » 06 Jan 2024, 13:14

I modified it to let me use Ctrl G as my hotkey as follows. Thanks for your willingness to help others!

Code: Select all

#Requires AutoHotkey v2.0
; single or double or long press
$^g::
{
    Static presses := 0
    squelch := 300 ;mSec between clicks in a double click -- they used 300


    hk := StrReplace(ThisHotkey, "$")    ; Strip the dollar sign
    hk1:= SubStr(hk, 1, 1)
    hk2:= SubStr(hk, 2, 1)

    If KeyWait(hk1, 'T.5') and KeyWait(hk2, 'T.5')                ; If key was quickly released,
    {
        SetTimer(done, -squelch)
        presses++ ;  then reset timer, and increment counter

    }
    Else
    {
        MsgBox "long press"
        presses := -1
    }

    done()
    {                             ; Key presses have ended
        Switch presses
        {
            Case 1: MsgBox "Single"                ; Single
            Case 2: MsgBox "Double"                ; Double
        }

        presses := 0
    }
}

Post Reply

Return to “Ask for Help (v2)”