AutoHotInterception (AHI)

repository·master·Indexed 20 days ago

https://github.com/evilc/autohotinterception

A library for AutoHotkey (v1 and v2) that uses the Interception driver to intercept and respond to input from specific hardware devices. It enables device-specific input remapping and automation by bypassing standard Windows input handling, allowing multiple keyboards or mice to be used independently via Context Mode or Subscription Mode.

Tokens
3.6K
Snippets
12
Records
19
Agent score
74%

What's inside AutoHotInterception

  1. What is AutoHotInterception (AHI)?

    master

    AutoHotInterception (AHI) is a library that allows you to execute AutoHotkey code in response to events from a specific keyboard or mouse. It can optionally block the native functionality of that device, meaning Windows will not see the original event. This allows you to use multiple keyboards or mice independently (e.g., pressing F1 on Keyboard A triggers one action, while F1 on Keyboard B triggers a different action).

    AHI supports both AHK v1 and AHK v2 and relies on the Interception driver by Francisco Lopez.

  2. How Interception identifies devices

    master

    Interception assigns a unique ID (1-20) to each device:

    • Devices 1-10: Always keyboards.
    • Devices 11-20: Always mice.

    Important Limitations:

    • These IDs are unique to the Interception driver and may change if you unplug/replug a device or resume from hibernation.
    • Warning: If a device's ID exceeds 10 (for keyboards) or 20 (for mice), the device will cease to function in both Windows and AHI until the next reboot.

    To identify devices, you can use the VID (Vendor ID) and PID (Product ID) for USB devices, or a Handle for legacy/PS2 devices.

  3. How Subscription Mode works

    master

    Subscription Mode bypasses the AutoHotkey hotkey system entirely. Instead, the Interception driver notifies you of input events via direct callbacks.

    Key Characteristics:

    • Full Support: Supports all forms of input, including Mouse Movement.
    • Precedence: Subscription Mode overrides Context Mode. If a key is subscribed to, Context Mode hotkeys for that key will not fire.
    • Granularity: SubscribeKey (specific key) overrides SubscribeKeyboard (all keys on a device).
    • Concurrency: Both keyboard and mouse subscription functions include an optional concurrent parameter:
      • false (Default): Callbacks are executed sequentially. A new callback will not fire until the previous one completes. Recommended for mouse movement.
      • true: A new thread is used for each callback. Warning: If your callback contains long-running loops, this can cause a thread buildup and memory leaks.
  4. How Context Mode works

    master

    Context Mode leverages AutoHotkey's native context-sensitive hotkeys. It is designed for Keyboard Keys and Mouse Buttons, but does not support Mouse Movement.

    To use it, you create a Context Manager object for a specific device ID. You then wrap your hotkeys in an #if (v1) or #HotIf (v2) block that checks the .IsActive property of that manager. This allows you to enable or disable a specific set of hotkeys based on whether a specific physical device is being used.

    ; AHK v2 Example
    #include Lib\AutoHotInterception.ahk
    
    AHI := AutoHotInterception()
    keyboard1Id := AHI.GetKeyboardId(0x04F2, 0x0112)
    cm1 := AHI.CreateContextManager(keyboard1Id)
    
    #HotIf cm1.IsActive
    ::aaa::JACKPOT
    1::
    {
    	ToolTip("KEY DOWN EVENT @ " A_TickCount)
    	return
    }
    
    1 up::
    {
    	ToolTip("KEY DOWN EVENT @ " A_TickCount)
    	return
    }
    #HotIf
  5. Warning: Risks of blocking input with Interception

    master

    Because the Interception driver operates below the Windows OS level, blocking input can be extremely disruptive.

    Critical Risks:

    • Blocking keys like Ctrl on your only keyboard can block system-level commands like CTRL+ALT+DEL.
    • Incorrect configurations can lock up all input, potentially requiring a physical hardware reset.
    • Using Subscription Mode with block enabled will totally prevent a key from working on that specific device.

    Best Practices:

    • Always have a secondary, unblocked keyboard or mouse available as an emergency backup.
    • If you are unsure, use Context Mode, which is safer than Subscription Mode.
    • Be cautious when setting scripts to run on Windows startup.
  6. Compile AHI scripts into EXEs

    master

    AHI scripts can be compiled into standalone executables. When you right-click a script and select "Compile", all required DLLs are packed inside the EXE.

    When the resulting EXE is run, it will automatically create a Lib folder containing the necessary DLLs in the same directory as the executable.

  7. Set up your AutoHotInterception working folder

    master

    To use AHI, you must organize your files into a specific structure. Follow these steps:

    1. Download AHI: Get a release from the GitHub releases page and extract it.
    2. Choose AHK Version: Extract either the AHK v1 or AHK v2 folder from the AHI zip to your working directory.
    3. Add AHI DLL: Extract AutoHotInterception.dll from the Common\lib folder of the AHI zip and place it into the lib folder of your working directory.
    4. Add Interception DLLs: From the Interception installer zip, copy the x86 and x64 folders (found inside the library folder) into your working directory's lib folder.
    5. Unblock DLLs: Windows often blocks downloaded DLLs. Right-click Unblocker.ps1 in your lib folder and select Run as Admin, or manually unblock the DLL files in their Properties menu.
    6. Verify: Run Monitor.ahk from your working folder to ensure the setup is correct.
  8. Prepare the x64 build environment

    master

    To build the x64 version of AutoHotInterception, you must manually provide the interception.dll file. Copy interception.dll from the library\x64 directory within the Interception zip file into the C#/Dependencies/x64/ folder.

    Note: You may also need to run unblocker.ps1 as an Administrator to ensure the DLL is not blocked by Windows security settings.

    # Run as Administrator if necessary
    .\unblocker.ps1
  9. Prepare Interception DLLs for building

    master

    To build the project, you must manually place the Interception DLLs into the C#/Dependencies/ directory. The directory structure must include x86 and x64 subfolders containing the DLLs extracted from the library folder of the Interception driver zip file.

    Note: You may need to run unblocker.ps1 as an Administrator to ensure the DLLs are not blocked by Windows security settings.

    C#/Dependencies/
    ├── x86/
    │   └── [Interception DLLs]
    └── x64/
        └── [Interception DLLs]
  10. Prepare dependencies for x86 builds

    master

    To build the x86 version of AutoHotInterception, you must manually provide the interception.dll file. Copy this file from the library\x86 directory within the Interception zip archive into the C#/Dependencies/x86/ folder.

    Note: You may also need to run unblocker.ps1 as an Administrator to ensure the DLL is not blocked by Windows security settings.

    # Run as Administrator if required
    .\unblocker.ps1
  11. Install the Interception driver

    master

    The Interception driver must be installed before using AHI.

    1. Download the Interception Driver.
    2. Open an Administrator Command Prompt (do not simply double-click the file).
    3. Run install-interception.exe.
    4. When prompted, execute install-interception.exe /install to complete the installation.
    # Run from an admin command prompt
    install-interception.exe /install
  12. Initialize the AutoHotInterception library

    master

    Depending on your AutoHotkey version, use the following initialization patterns.

    ### AHK v1
    ```autohotkey
    #Persistent
    #include Lib\AutoHotInterception.ahk
    
    global AHI := new AutoHotInterception()

    AHK v2

    Persistent
    #include Lib\AutoHotInterception.ahk
    
    global AHI := AutoHotInterception()