• Stars
    star
    122
  • Rank 290,670 (Top 6 %)
  • Language AutoHotkey
  • License
    MIT License
  • Created over 6 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

An AHK library for Long Press / Multi tap / Multi tap and hold

TapHoldManager

An AutoHotkey library that allows you to map multiple actions to one key using a tap-and hold system
Long press / Tap / Multi Tap / Tap and Hold / Multi Tap and Hold etc are all supported

Getting Help

Use the TapHoldManager Discussion Thread on the AHK Forums

Normal Usage

Setup

  1. Download a zip from the releases page
  2. Extract the zip to a folder of your choice
  3. Run the Example script

Usage

Include the library

#include Lib\TapHoldManager.ahk

Instantiate TapHoldManager

thm := new TapHoldManager()

Add some key(s)

thm.Add("1", Func("MyFunc1"))
thm.Add("2", Func("MyFunc2"))

Add your callback function(s)

MyFunc1(isHold, taps, state){
	ToolTip % "1`n" (isHold ? "HOLD" : "TAP") "`nTaps: " taps "`nState: " state
}

MyFunc2(isHold, taps, state){
	ToolTip % "2`n" (isHold ? "HOLD" : "TAP") "`nTaps: " taps "`nState: " state
}

IsHold will be true if the event was a hold. If so, state will holl 1 for pressed or 0 for released.
If IsHold is false, the event was a tap, and state will be -1
In either case, taps holds the number of taps which occurred.
For example, if I double-tap, IsHold will be false, and taps will be 2.
If I double-tapped and held on the second tap, then on press the function would be fired once with IsHold as true, taps would as 2 and state as 1. When the key is released, the same but state would be 0

Instantiating TapHoldManager

thm := new TapHoldManager([ <tapTime := -1>, holdTime := -1, <maxTaps := -1>, <prefix := "$">, <window := ""> ])

tapTime The amount of time after a tap occured to wait for another tap.
Defaults to 150ms.
holdTime The amount of time that you need to hold a button for it to be considered a hold.
Defaults to the same as tapTime.
maxTaps The maximum number of taps before the callback will be fired.
Defaults to infinite.
Setting this value to 1 will force the callback to be fired after every tap, whereas by default if you tapped 3 times quickly it would fire the callback once and pass it a taps value of 3, it would now be fired 3 times with a taps value of 1.
If maxTaps is 1, then the tapTime setting will have no effect.
prefix The prefix used for all hotkeys, default is $
window An AHK WinTitle string that defines which windows the hotkey will take effect in
For example, to make Hotkeys only work in Notepad, you could use:
thm := new TapHoldManager(,,,,"ahk_exe notepad.exe")

You can pass as many parameters as you want.
thm := new TapHoldManager()
thm := new TapHoldManager(100, 200, 1, "$*")

When specifying parameters, you can omit the parameter (or use -1) to leave that parameter at it's default.
For example, if you only wish to alter the prefix (3rd) parameter, you could do:
thm := new TapHoldManager(-1, -1, -1, "$*") Or
thm := new TapHoldManager(,,, "$*")

Adding Hotkeys

thm.Add("<keyname>", <callback (function object)> [,<tapTime := -1>, holdTime := -1, <maxTaps := -1>, <prefix := "$">, <window := "">])
When adding keys, you can also add the same parameters to the end to override the manager's default settings
thm.Add("2", Func("MyFunc2"), 300, 1000, 1, "~$")

Enabling or Disabling Hotkeys

Hotkeys can be disabled by calling PauseHotkey and passing the name of the key to pause:
thm.PauseHotkey("1")

Hotkeys can be re-enabled by calling ResumeHotkey and passing the name of the key to resume:
thm.PauseHotkey("1")

Hotkeys can be removed by calling RemoveHotkey and passing the name of the key to remove:
thm.RemoveKey("1")

Logic Flowchart

(Note that the firing of the function on key release during a hold is omitted from this diagram for brevity) flowchart

Example Timelines

timeline

Minimizing response times

To make taps fire as quickly as possible, set tapTime as low as possible.
If you do not require multi-tap or multi-tap-and-hold, then setting maxTaps to 1 will make taps respond instantly upon release of the key. In this mode, the tapTime setting will have no effect.
To make holds fire as quickly as possible, setting holdTime as low as possible will help. maxTaps will not affect response time of holds, as they always end a sequence.

Integration with the Interception driver (Multiple Keyboard support)

TapHoldManager can use the Interception driver to add support for per-keyboard hotkeys - you can bind TapHoldManager to keys on a second keyboard, and use them completely independently of your main keyboard.

Interception Setup

  1. Set up my AutoHotInterception AHK wrapper for Interception.
    Get the interception example running, so you know AHK can speak to interception OK.
  2. Download the latest release of TapHoldManager from the releases page and extract it to a folder of your choice.
  3. You need to add the files from TapHoldManager's lib folder to AutoHotInterception's lib folder.
    (Or, you can make sure the contents of both lib folders are in My Documents\AutoHotkey\Lib)
  4. Enter the VID / PID of your keyboard(s) into the Interception example script and run

AutoHotInterception modes

TapHoldManager works in both AutoHotInterception modes - "Context" and "Subscription" modes.

AutoHotInterception Context Mode

Just before you call Add, set the context for hotkeys using hotkey, if, <context var> which matches that which your AHK Context Callback sets.
For example:

#include Lib\TapHoldManager.ahk
#include <AutoHotInterception>

AHI := new AutoHotInterception()
keyboardId := AHI.GetKeyboardId(0x03EB, 0xFF02)
cm1 := AHI.CreateContextManager(keyboardId)

thm := new TapHoldManager()

hotkey, if, cm1.IsActive
thm.Add("1", Func("MyFunc1"))
hotkey, if
return

MyFunc1(isHold, taps, state){
    Tooltip % "IsHold: " isHold "`nTaps: " taps "`nState: " state
}

#if cm1.IsActive ; Hotkey, if needs a context to match, even if it is empty
#if

AutoHotInterception Subscription Mode

A wrapper is included which extends the TapHoldManager class and replaces the hotkey bind code with Interception bind code.

As well as including the TapHoldManager library, include InterceptionTapHold.ahk and AutoHotInterception.ahk.
There are many ways to do this - you could either have one Lib folder next to the script containing the contents of both the AHI Lib folder and the THM Lib folder, and use:

#include Lib\TapHoldManager.ahk
#include Lib\InterceptionTapHold.ahk
#include Lib\AutoHotInterception.ahk

Or, copy the contents of both the AHI and THM Lib folders to C:\My Documents\AutoHotkey\Lib, and use

#include <AutoHotInterception>
#include <InterceptionTapHold>
#include <TapHoldManager>

Instantiate AutoHotInterception:
AHI := new AutoHotInterception()

Get the ID of your device:
keyboard1Id := AHI.GetKeyboardId(0x03EB, 0xFF02)

Instantiate InterceptionTapHold instead of TapHoldManager and pass in the AHI instance and the id of the device:
THI1 := new InterceptionTapHold(<AHI Instance>, <Device ID> [, <tapTime>, <holdTime>, <maxTaps>, <block>])
eg ITH1 := new InterceptionTapHold(AHI, keyboard1Id)

Required Parameters
AHI Instance = An Instance of the AutoHotInterception class
Device ID = The ID of the device to subscribe to

Optional Parameters
The usual THM parameters, plus:
block = whether or not to block the input. Defaults to true.

Note: Use one manager per keyboard.

ITH1 := new InterceptionTapHold(AHI, keyboardId1)
ITH2 := new InterceptionTapHold(AHI, keyboardId2)

More Repositories

1

AutoHotInterception

An AutoHotkey wrapper for the Interception driver
C#
694
star
2

UCR

Universal Control Remapper [ALPHA]
AutoHotkey
194
star
3

AHK-Universal-Joystick-Remapper

An application to remap one or more physical sticks to a virtual stick, with modifications such as axis inversion, deadzones, sensitivity etc. Uses the vJoy virtual joystick library.
AutoHotkey
86
star
4

AutoHotStreamDeck

An AutoHotkey wrapper for interacting with the Elgato Stream Deck
C#
73
star
5

ADHD-AHK-Dynamic-Hotkeys-for-Dummies

An AutoHotKey library for creating GUI scripts with user-configurable, persistent settings.
AutoHotkey
66
star
6

HotVoice

Adds Speech Recognition support to AutoHotkey, via a C# DLL
AutoHotkey
56
star
7

AHK-CvJoyInterface

A Simple to use class for AutoHotkey to interface with Shaul's vJoy
AutoHotkey
39
star
8

AHK-CGDipSnapShot

An AHK library for working with snapshots of a portion of the screen (compare, easy conversion etc) using the GDI+ library
AutoHotkey
28
star
9

AppFactory

A library for creating AHK Gui Apps with configurable hotkeys and settings.
AutoHotkey
27
star
10

JoystickWrapper

A C# DLL wrapper using SharpDX, to replace AHK's joystick functionality
C#
25
star
11

IOWrapper

Universal Control Remapper 2.0 (Back End)
C#
25
star
12

AHK-ViGEm-Bus

A wrapper for AHK that drives the ViGEm bus to create virtual console controllers
AutoHotkey
25
star
13

MW5HOTAS

A tool and guide to assist configuring HOTAS controllers in Mechwarrior 5
C#
24
star
14

AHK-Random-Musings

A random collection of test scripts, proof-of-concepts etc.
AutoHotkey
18
star
15

RollMouse

Emulate a trackball "spin" by lifting an optical mouse mid-movement
AutoHotkey
16
star
16

RDPTools

An AHK library for automating RDP client connections
AutoHotkey
15
star
17

Fire-Control

A multi-function macro for Mechwarrior Online
AutoHotkey
11
star
18

SequenceSender

A library for AutoHotkey to make it easy to send (optionally repeating) sequences of characters
AutoHotkey
11
star
19

CGui

A wrapper for AHK's GUI functionality, Experimental!
AutoHotkey
10
star
20

VTOLVRPhysicalInput

A mod for VTOL VR to allow using physical input devices
C#
9
star
21

WootingAHK

An AutoHotkey wrapper for the Wooting Keyboard analog / RGB APIs
AutoHotkey
9
star
22

CHotkeyControl

AutoHotkey
9
star
23

RADical

An AHK library for Rapid Application Development of GUI applications
AutoHotkey
8
star
24

SpaceMouse-HidLibrary

A library to read the SpaceMouse
C#
8
star
25

HotClass

A class that enhances AutoHotkey's input detection (ie hotkeys) capabilities
AutoHotkey
7
star
26

AHK-Library-Setup

Tool for AHK Library writers to ensure people using their library are set up correctly.
AutoHotkey
7
star
27

AHK-CHID

A class-based implementation of HID functions using HotkeyIt's _Struct
AutoHotkey
6
star
28

MicroTimer

A DLL Wrapper for Ken Loveday's MicroLibrary, with AutoHotkey interop demo
C#
6
star
29

7DTD-Loot-Viewer

C#
5
star
30

CHotkeyHandler

A class to allow end-users to easily bind hotkeys to script actions.
AutoHotkey
5
star
31

SourceGrab

An AHK Class to grab the source code of a web page
AutoHotkey
5
star
32

AHK-HID-Hotkeys

A class to replace AHK's hotkeys with a HID-based system not subject to the limitations of AHK's hotkeys.
AutoHotkey
5
star
33

HIDuino

Experiments with Arduino based HID manipulation
C#
4
star
34

OneSwitch-Utilities

A set of utilitie to assist disabled people operate computers. In association with OneSwtich.
AutoHotkey
4
star
35

CScrollGui

A Class that enables scrollbars for GUIs. Same as Just Me's code, but using _Struct / WinStructs
AutoHotkey
4
star
36

UJRC

UJRC - Universal Joystick Remapper Companion - An AHK library to make it easy to map multiple functions onto a limited number of joystick buttons through the use of shift states.
AutoHotkey
4
star
37

BaTSHit

Betaflight Thrust Stand Helper
AutoHotkey
4
star
38

NodeNetworkTests

A bunch of code playing with WouterDeK's NodeNetwork
C#
3
star
39

ProfileHandler

A library for adding Per-Profile settings to AHK GUI scripts
AutoHotkey
3
star
40

Chat

A program to automatically type predefined strings
AutoHotkey
3
star
41

SpaceMouse-HidSharp

A library to read the SpaceMouse
C#
3
star
42

NodeNetworkMefCalculator

An example of using MEF to load Nodes for WouterDeK's NodeNetwork
C#
3
star
43

Love2D-Joystick-Tester

A joystick input tester for Love2D.
Lua
2
star
44

LogSync

A tool to view multiple logs, synchronized in time and with synchronized scrollbars
C#
2
star
45

SockPuppet

A Remote Control library for AHK using TCP Sockets
AutoHotkey
2
star
46

ADHD-Analog-to-Digital

An experiment in analogue to digital - using an axis to spam space at varying rates
AutoHotkey
2
star
47

TitanWrapper

A C# wrapper for the Titan One API, with sample AutoHotkey scripts
C#
2
star
48

MarlinProbeAssistedLeveler

A tool to physically level a 3D printer's bed using a BLTouch probe
C#
1
star
49

Spreadsheet-Barcode-Scanner

A utility to make using a barcode scanner to find items in a spreadsheet easier
AutoHotkey
1
star
50

relative_throttle

AutoHotkey
1
star
51

InputWrapper

A plugabble Input Detection wrapper using MEF
C#
1
star
52

MWO-Intelligent-Zoom

An Autohotkey script to provide proper zoom controls in Mechwarrior Online
AutoHotkey
1
star
53

Pad-Throttle

Makes a joypad such as an XBOX controller behave like a throttle and rudder pedals
AutoHotkey
1
star
54

MWO-Absolute-Aim

A utility to enhance absolute stick aiming in MWO
AutoHotkey
1
star
55

xbox-360-controller-custom

Automatically exported from code.google.com/p/xbox-360-controller-custom
C++
1
star
56

MWO-Auto-Ready

A Script for MWO that automatically clicks ready for you
AutoHotkey
1
star