1

I found this script from https://autohotkey.com/boards/viewtopic.php?f=5&t=51596 it works perfectly. I like that it is easy to understand and change your desired key easily.

The thing is:

  1. It fires "a" when it runs for the first time without me pressing any key

  2. I wanted to set other keys using this method but It only works for the first key assignment. I copied the working script and tried to modify the variable names not to interfere with each other and pasted it below the very bottom "return" but anything that I add below the KeyWait, {%Key%} tapCount = Return doesn't work

Here's the currently modified partially working script, what am I missing here

Key = a ;Main Keybind
tLength = -300 ;Time before key is pressed
tapSingle = a ;Single Tap Keybind
tapDouble = b ;Double Tap Keybind
tapHold = c ;Hold Keybind
;----------------------------------------------------------
Hotkey, $%Key%, startTimer
startTimer:
If !endTimer
SetTimer, endTimer, %tLength%
tapCount++
Return
endTimer:
If GetKeyState(Key, "P") and tapCount < 2
Send, {%tapHold%}
Else If tapCount = 2
Send, {%tapDouble%}
Else If tapCount = 1
Send, {%tapSingle%}
KeyWait, {%Key%}
tapCount =
Return

==ANYTHING BELOW IS NOT WORKING==

bKey = b ;Main Keybind
btLength = -300 ;Time before key is pressed
btapSingle = w ;Single Tap Keybind
btapDouble = a ;Double Tap Keybind
btapHold = s ;Hold Keybind
;----------------------------------------------------------
Hotkey, $%bKey%, bstartTimer
bstartTimer:
If !bendTimer
SetTimer, bendTimer, %btLength%
btapCount++
Return
bendTimer:
If GetKeyState(bKey, "P") and btapCount < 2
Send, {%btapHold%}
Else If btapCount = 2
Send, {%btapDouble%}
Else If btapCount = 1
Send, {%btapSingle%}
KeyWait, {%bKey%}
btapCount =
Return

1 Answer 1

2

There are some syntax issues (eg, tapCount = on the second-last line). I've re-written the script updating it to AutoHotkey v2 and using a function so that multiple key combinations can be bound without needing to copy and paste lines.

TripleBind(mainKey, msec, singleKey, doubleKey, holdKey)
{
  ProcessPress(key)
  {
    static count := 0
    held := true
    ++count
    AfterTime()
    {
      if (held) {
        Send holdKey
      } else if (count == 1) {
        Send singleKey
      } else {
        Send doubleKey
      }
      count := 0
    }

    if (count == 1) {
      SetTimer AfterTime, -1 * msec
      KeyWait mainKey
      held := false
    }
  }

  Hotkey "$" . mainKey, ProcessPress
}

TripleBind("a", 300, "a", "b", "c")
TripleBind("b", 300, "w", "a", "s")

To make it easier to find this updated script from a search engine, here is original question in the link quoted:

I've been trying to do this for an hour or so now, but I'm not really getting anywhere.

Essentially, I have a key. For argument's sake we'll say it's a

When a is tapped, I want it to wait 300ms and then press a. When a is tapped and then tapped again within 300ms, I want it to then press b 300ms after the first tap. When a is held down for 300ms, I want it to press c once.

What this means is there will always be a character sent after 300ms, and depending on whether or not the button was pressed once, twice, or held for that period will determine the key that is output.

The actual use case for this script is to give a single key foot pedal multiple bindings depending on how I press it.

If anyone could give me any advice on this, I would hugely appreciate it.

1
  • I can not thank you enough, I spent weeks trying to make it work. your AHK V2 script is way cleaner and much responsive. Thank you again! Commented Apr 8 at 17:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .