Triple actions for single key - Tap, Double Tap, Hold

Get help with using AutoHotkey and its commands and hotkeys
Jademalo
Posts: 4
Joined: 27 May 2018, 18:03

Triple actions for single key - Tap, Double Tap, Hold

05 Jul 2018, 20:10

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.
I can't imagine this being too complex a script, but it's just a tiny bit beyond my ability. I've managed to get a double tap for a character working using;

Code: Select all

a::
if (A_PriorHotkey <> "a" or A_TimeSincePriorHotkey > 400)
{
	KeyWait, a
	return
}	

	send, b
But I couldn't get the original key to be pressed if I didn't double tap. I also had absolutely no idea how to go about doing the hold for a third key.

Thanks!
Rohwedder
Posts: 3974
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Triple actions for single key - Tap, Double Tap, Hold

06 Jul 2018, 00:53

Hallo,
try:

Code: Select all

$a::
If !Ta
	SetTimer, Ta, -300
Ta++
Return
Ta:
If GetKeyState("a","P")
	Send, c
Else If Ta = 2
	Send, b
Else If Ta = 1
	Send, a
KeyWait, a ;(you want it to press c once)
Ta = 
Return
Jademalo
Posts: 4
Joined: 27 May 2018, 18:03

Re: Triple actions for single key - Tap, Double Tap, Hold

06 Jul 2018, 07:05

Thank you!
This works, but only once. If I try to press the key again it just gives me no output.

EDIT:

Never mind, I forgot to scroll down in the code box. I'm a dope. This is absolutely perfect, thank you!

One small tweak I have made however is to change

Code: Select all

	If GetKeyState("a","P") and Ta != 2
This just means if I'm slow releasing from the double press of b, then it doesn't instead type c.
mikagenic
Posts: 73
Joined: 16 Sep 2014, 18:26

Re: Triple actions for single key - Tap, Double Tap, Hold

29 Nov 2020, 16:46

I ended up with a different approach to solving the same issue, the code is a bit longer but I found it more responsive and reliable:

Code: Select all

~$RShift::
  if (!RShiftState) {
    RShiftState:=0
  }
  if (RShiftState = 0) { 
    SetTimer, RShiftHoldTimerLabel, -500
    RShiftState:=1    
  }
  return
~$RShift up::
  SetTimer, RShiftHoldTimerLabel, Delete
  SetTimer, RShiftSingleClickTimerLabel, Delete
  If (A_PriorKey <> "RShift") { ; combination - do nothing
    RShiftState:=0
  } else if (RShiftState = 1) { 
    SetTimer, RShiftSingleClickTimerLabel, -170
    RShiftState:=2
  } else if (RShiftState = 2) { 
    RShiftState:=0
    MsgBox, DOUBLECLICK
  } else { ; hold
    RShiftState:=0
  }
  Return
RShiftSingleClickTimerLabel:
  SetTimer, RShiftHoldTimerLabel, Delete
  RShiftState:=0
  MsgBox, SINGLECLICK
  return
RShiftHoldTimerLabel:
  RShiftState:=3
  If (A_PriorKey = "RShift") {
    MsgBox, HOLD
  }
  return

Return to “Ask For Help”

Who is online

Users browsing this forum: malcev, WatsonEnterprises and 32 guests