AutoHotInterception/README.md

355 lines
17 KiB
Markdown
Raw Normal View History

2018-07-02 19:38:41 +00:00
![GitHub release](https://img.shields.io/github/release/evilc/autohotinterception.svg)
![Github All Releases](https://img.shields.io/github/downloads/evilc/autohotinterception/total.svg)
2018-07-02 19:38:41 +00:00
[![Discord](https://img.shields.io/discord/330423308103319562.svg)](https://discord.gg/9d3DNN4)
![Github commits (since latest release)](https://img.shields.io/github/commits-since/evilc/autohotinterception/latest.svg)
2018-03-06 18:03:45 +00:00
# AutoHotInterception
2018-03-07 01:45:09 +00:00
AutoHotInterception (AHI) allows you to execute AutoHotkey code in response to events from a *specific* keyboard or mouse, whilst (optionally) blocking the native functionality (i.e. stopping Windows from seeing that keyboard or mouse event).
In other words, you can use a key on a second (or third, or fourth...) keyboard to trigger AHK code, and that key will not be seen by applications. You can use the *same key* on multiple keyboards for individual actions.
2018-05-02 09:24:59 +00:00
Keyboard Keys, Mouse Buttons and Mouse movement (Both Relative and Absolute modes) are supported.
2018-03-07 01:45:09 +00:00
2018-04-02 17:20:48 +00:00
AHI uses the Interception driver by Francisco Lopez
# Getting Help
2019-03-05 22:30:27 +00:00
## [AHI Discussion Thread on the AHK forums](https://autohotkey.com/boards/viewtopic.php?f=6&t=45307)
## [Discord Channel](https://discord.gg/sFPMv86)
2018-03-07 01:45:09 +00:00
2018-03-31 22:25:36 +00:00
------
2018-03-13 00:11:15 +00:00
# WARNING
2018-03-13 00:26:13 +00:00
**TAKE CARE** when using this code. Because Interception is a driver, and sits below windows proper, blocking with Interception goes so deep that it can even block CTRL+ALT+DEL etc. As such, it is entirely possible to lock up all input, or at least make life a little difficult.
In general, worst-case scenario would require use of the reset button.
2018-03-13 00:14:14 +00:00
For example, using Subscription Mode with `block` enabled will **totally** block that key from working on that keyboard.
So if you block `Ctrl` on your only keyboard, you just blocked CTRL+ALT+DEL.
2018-06-16 16:07:47 +00:00
The best insurance policy is to have another keyboard or mouse handy, one that you don't block.
2018-03-13 00:26:13 +00:00
Be wary of making scripts using this code run on startup. Know how to enter "Safe Mode" in windows and disable startup of the scripts. Know mouse alternatives to emergency keyboard actions (Right click on clock for Task Manager!)
2018-03-13 00:10:46 +00:00
As they say - ***With great power comes great responsibility***.
2018-03-13 00:28:27 +00:00
If this all scares you and you don't really understand it, then TL/DR is you should probably stick to "Context Mode", it's safer.
2018-03-13 00:09:44 +00:00
2018-03-31 22:25:36 +00:00
------
# Device IDs / VIDs PIDs etc
2018-04-01 18:55:54 +00:00
Interception identifies unique devices by an ID. This is a number from 1..20.
2018-03-31 22:25:36 +00:00
Devices 1-10 are always keyboards
2018-04-01 18:55:54 +00:00
Devices 11-20 are always mice
2018-03-31 22:25:36 +00:00
This ID scheme is totally unique to Interception, and IDs may change as you plug / unplug devices etc.
On PC, devices are often identified by VendorID (VID) and ProductID (PID). These are identifiers baked into the hardware at time of manufacture, and are identical for all devices of the same make / model.
Most AHI functions (eg to Subscribe to a key etc) use an Interception ID, so some handy functions are provided to allow you to find the (current) Interception ID of your device, given a VID / PID.
If you are unsure of what the VID / PID of your device is (or even if Interception can see it), you can use the included Monitor script to find it.
2019-08-23 21:21:40 +00:00
You will need to know the VID / PID of at least one of your devices in order to do anything with AHI.
## Monitor App
This handy tool allows you to check if AHI is working, and also to find the VID/PID or DeviceHandle of your devices.
You can use the handy "Copy" buttons to copy the VID/PID or DeviceHandle of the device to the clipboard.
When using the monitor app, **DO NOT** tick all devices at once, as if it crashes, it will lock up all devices.
Instead, tick one at a time and see if it your device.
![](https://github.com/evilC/AutoHotInterception/blob/master/Monitor.png)
2018-03-31 22:25:36 +00:00
------
2019-08-09 19:40:03 +00:00
# Known Issues
If you unplug / replug a device, or go into hibernate and resume, the Interception ID of a device will increase by 1.
If the ID of a device goes above 10 (For keyboards) or 20 (For Mice), **The device will completely cease to function until the next reboot** - not only in AutoHotInterception, but in Windows also.
There is nothing I can do to fix this issue, it is a limitation of the Interception driver
------
2018-03-07 01:45:09 +00:00
# Setup
2018-03-31 22:25:36 +00:00
2018-03-07 01:45:09 +00:00
1. Download and install the [Interception Driver](http://www.oblita.com/interception)
2019-08-20 16:02:29 +00:00
Note that you **must** run `install-interception.exe` at an admin command prompt (**Not double-click it**) - once you do so, it will instruct you to execute `install-interception.exe /install` to actually perform the install.
Here is a GIF showing the process:
![](https://github.com/evilC/AutoHotInterception/blob/master/InterceptionInstall.gif)
2018-03-29 13:44:16 +00:00
2. Download an AHI release from the [releases page](https://github.com/evilC/AutoHotInterception/releases) and extract it to a folder.
DO NOT use the "Clone or Download" link on the main page.
This is the folder where (at least initially) you will be running scripts from.
2019-02-26 19:54:37 +00:00
It contains a number of sample `.ahk` scripts and a `lib` folder, which contains all the AHI libraries.
2019-02-24 18:10:08 +00:00
3. In the Interception installer zip, there is a `library` folder containing `x86` and `x64` folders.
2019-02-26 19:54:37 +00:00
Copy both of these folders into the AHI `lib` folder that you created in step (3) - the folder structure should end up looking like:
```
AHI Root Folder
Monitor.ahk
etc...
Lib
AutoHotInterception.ahk
AutoHotInterception.dll
CLR.ahk
Unblocker.ps1
etc..
x86
interception.dll
x64
interception.dll
```
2018-03-30 15:10:22 +00:00
4. Right-click `Unblocker.ps1` in the lib folder and select `Run as Admin`.
This is because downloaded DLLs are often blocked and will not work.
This can be done manually by right clicking the DLLs, selecting Properties, and checking a "Block" box if it exists.
2019-08-23 21:21:40 +00:00
5. Edit one of the example remapping scripts, replacing the VID/PID(s) with that of your device (Use the Monitor app to find it) and run it to make sure it works.
6. (Optional) The contents of the `lib` folder can actually be placed in one of the AutoHotkey lib folders (eg `My Documents\AutoHotkey\lib` - make it if it does not exist), and the `#include` lines of the sample scripts changed to `#include <AutoHotInterception>`, to enable your AHI scripts to be in any folder, without each needing it's own copy of the library files.
2018-03-07 01:58:23 +00:00
2018-03-31 22:25:36 +00:00
------
2018-03-28 20:10:29 +00:00
2018-03-07 01:58:23 +00:00
# Usage
2018-03-07 21:05:21 +00:00
## Initializing the Library
2018-03-07 01:58:23 +00:00
Include the library
```
#Persistent ; (Interception hotkeys do not stop AHK from exiting, so use this)
#include Lib\AutoHotInterception.ahk
```
Initialize the library
```
global AHI := new AutoHotInterception()
2018-03-31 22:25:36 +00:00
```
2018-11-02 19:56:41 +00:00
2018-11-02 19:57:08 +00:00
*Note*
2018-11-02 19:56:41 +00:00
The `AHI` variable is an AHK class that makes it easy to interact with the AutoHotInterception DLL (Which itself then interacts with the Interception dll). For example, it wraps `GetDeviceList()` to make it return a normal AHK array. Most of the time you will not need it.
For advanced users, if you wish to directly communicate with the AHI DLL (eg for best possible performance), you can call `AHI.Instance` instead of `AHI` for most functions (eg when sending of synthesized input using `SendMouseMove`).
2018-03-30 18:34:31 +00:00
```
2018-03-30 18:29:06 +00:00
AHI := new AutoHotInterception()
2018-03-31 22:25:36 +00:00
AHI.Instance.SendMouseMove(...)
```
2018-03-30 18:29:06 +00:00
2019-07-09 13:59:25 +00:00
## Misc Commands
2019-07-18 16:52:01 +00:00
### SetState
`SetState(true|false)`
2019-07-09 13:59:25 +00:00
Turns on or off all subscriptions (Starts on)
Where `true` is on, `false` is off.
2019-07-18 16:52:01 +00:00
eg `AHI.SetState(false)`
2018-03-28 20:10:29 +00:00
## Finding Device IDs
2018-07-02 18:50:08 +00:00
### USB Devices
2018-03-30 18:24:31 +00:00
In most cases, you will want to hard-wire a script to a specific VID/PID - in this instance, use one of the following methods.
For all these methods, if you have multiple identical VID/PID devices, you can specify an `instance` (Starts from 1).
#### GetDeviceId
2018-03-30 18:14:44 +00:00
`AHI.GetDeviceId(<isMouse>, <VID>, <PID> [,<instance = 1>] )`
2018-03-28 20:10:29 +00:00
Where `isMouse` is `true` if you wish to find a mouse, or `false` if you wish to find a keyboard.
2018-03-30 18:14:44 +00:00
eg `AHI.GetDeviceId(false, 0x04F2, 0x0112)` to find a keyboard with VID 0x04F2 and PID 0x0112
2018-03-30 18:24:31 +00:00
#### GetKeyboardId
`AHI.GetKeyboardId(<VID>, <PID> [,<instance = 1>] )`
#### GetMouseId
`AHI.GetMouseId(<VID>, <PID> [,<instance = 1>] )`
2018-03-28 20:10:29 +00:00
2018-07-02 18:50:08 +00:00
### PS/2 and other Legacy devices (Can also apply to Laptops)
Some devices (eg older machines with PS/2 interfaces, or some laptops) may not use USB, so these will not have a VID and PID.
In this case, use the monitor app (Or `GetDeviceList()`) to findle out the "Handle" of your device, and get it's ID from that.
#### GetDeviceIdFromHandle
`AHI.GetDeviceIdFromHandle(<isMouse>, <handle> [,<instance = 1>] )`
2018-07-02 18:51:47 +00:00
This works in the same way as `GetDeviceId` above, except you pass a string containing the handle.
eg `AHI.GetDeviceIdFromHandle(false, "ACPI\PNP0303")` to find a keyboard with the handle `ACPI\PNP0303`
2018-07-02 18:50:08 +00:00
#### GetKeyboardIdFromHandle
`AHI.GetKeyboardIdFromHandle(<handle> [,<instance = 1>] )`
#### GetMouseIdFromHandle
`AHI.GetMouseIdFromHandle(<handle> [,<instance = 1>] )`
2018-03-30 14:58:40 +00:00
### Getting a list of devices
2018-03-30 18:14:44 +00:00
If you wish to get a list of all available devices, you can call `AHI.GetDeviceList()`, which will return an array of `DeviceInfo` objects, each of which has the following properties:
2018-03-28 20:10:29 +00:00
```
Id
isMouse
Vid
Pid
2018-07-02 18:50:08 +00:00
Handle
2018-03-28 20:10:29 +00:00
```
2018-03-08 00:11:38 +00:00
2018-03-31 22:25:36 +00:00
## Input Detection
AHI has two input detection modes - *Context Mode* and *Subscription Mode*, and both can be used simultaneously.
2018-03-07 01:58:23 +00:00
2018-03-08 00:11:38 +00:00
### Context mode
2018-03-07 21:05:21 +00:00
Context mode is so named as it takes advantage of AutoHotkey's [Context Sensitive Hotkeys](https://autohotkey.com/docs/Hotkeys.htm#Context).
2018-03-25 16:29:55 +00:00
As such, only Keyboard Keys and Mouse Buttons are supported in this mode. Mouse Movement is not supported.
2018-03-30 18:16:45 +00:00
2018-03-31 22:25:36 +00:00
In context mode, you create a *Context Manager* object which turns on/off a set of AHK hotkeys for you.
2018-03-30 18:14:44 +00:00
You wrap your hotkeys in an #if block which is controlled by the manager.
2018-03-07 21:05:21 +00:00
2018-03-30 18:16:45 +00:00
Create a Context Manager for the keyboard or mouse, pass it the Interception ID of the device.
2018-03-30 18:20:14 +00:00
Then Create your hotkeys, wrapped in an `#if` block that checks the `.IsActive` property of your Context Manager
(Complete, working script)
2018-03-07 21:05:21 +00:00
```
2018-03-30 18:20:14 +00:00
#include Lib\AutoHotInterception.ahk
2018-03-30 18:14:44 +00:00
keyboard1Id := AHI.GetKeyboardId(0x04F2, 0x0112)
cm1 := AHI.CreateContextManager(keyboard1Id)
2018-03-07 21:05:21 +00:00
2018-03-30 18:14:44 +00:00
#if cm1.IsActive ; Start the #if block
2018-03-07 21:05:21 +00:00
::aaa::JACKPOT
1::
ToolTip % "KEY DOWN EVENT @ " A_TickCount
return
1 up::
ToolTip % "KEY UP EVENT @ " A_TickCount
return
2018-03-30 18:14:44 +00:00
#if ; Close the #if block
2018-03-07 21:05:21 +00:00
```
You can remove a Context Manager using `AHI.RemoveContextManager(keyboard1Id)`
2018-03-08 00:11:38 +00:00
### Subscription mode
2018-03-08 00:09:00 +00:00
In Subscription mode, you bypass AHK's hotkey system completely, and Interception notifies you of key events via callbacks.
2018-03-25 16:29:55 +00:00
All forms of input are supported in Subscription Mode.
2018-03-12 20:46:36 +00:00
Subscription Mode overrides Context Mode - that is, if a key on a keyboard has been subscribed to with Subscription Mode, then Context Mode will not fire for that key on that keyboard.
SubscribeKey overrides SubscribeKeyboard - that is, if you have subscribed to all keys and a specific key on the same keyboard, then if you press the specific key, it's callback will fire and the callback for SubscribeKeyboard will not.
Each Subscribe endpoint also has a corresponding Unsubscribe endpoint, which removes the subscription and any block associated with it.
2018-03-07 21:05:21 +00:00
2018-03-28 20:24:46 +00:00
#### Subscribing to Keyboard keys
2019-12-15 22:22:22 +00:00
##### Subscribe to a specific key on a specific keyboard
2019-05-15 12:45:54 +00:00
`SubscribeKey(<deviceId>, <scanCode>, <block>, <callback>, <concurrent>)`
2019-02-10 14:36:39 +00:00
`UnsubscribeKey(<deviceId>, <scanCode>)`
2019-12-15 22:22:22 +00:00
eg
`AHI.SubscribeKey(keyboardId, GetKeySC("1"), true, Func("KeyEvent"))`
2018-03-07 01:58:23 +00:00
2018-03-07 21:05:21 +00:00
Callback function is passed state `0` (released) or `1` (pressed)
2018-03-07 01:58:23 +00:00
```
KeyEvent(state){
ToolTip % "State: " state
}
```
Parameter `<concurrent>` is optional and is <b>false</b> by default meaning that all the events raised for that key will be handled sequentially (i.e. callback function will be called on a single thread). If set to <b>true</b>, a new thread will be created for each event and the callback function will be called on it.
2019-12-15 22:22:22 +00:00
##### Subscribe to all keys on a specific keyboard
`SubscribeKeyboard(<deviceId>, <block>, <callback>, <concurrent>)`
eg
`AHI.SubscribeKeyboard(keyboardId, true, Func("KeyEvent"))`
Callback function is passed scancode of pressed key and state
```
KeyEvent(code, state){
ToolTip % "Keyboard Key - Code: " code ", State: " state
}
```
2018-03-28 20:24:46 +00:00
#### Subscribing to Mouse Buttons
2019-12-15 22:22:22 +00:00
##### Subscribing to a specific button on a specific mouse
`SubscribeMouseButton(<deviceId>, <button>, <block>, <callback>, <concurrent>)`
2019-02-10 14:36:39 +00:00
`UnsubscribeMouseButton(<deviceId>, <button>)`
2018-03-25 16:29:55 +00:00
Where `button` is one of:
```
0: Left Mouse
1: Right Mouse
2: Middle Mouse
3: Side Button 1
4: Side Button 2
5: Mouse Wheel (Vertical)
6: Mouse Wheel (Horizontal)
2018-03-31 22:25:36 +00:00
```
For Mouse Wheel events, the `<state>` parameter will be `1` for Wheel Up / Right and `-1` for Wheel Down / Left
2018-03-31 22:25:36 +00:00
2018-03-25 16:29:55 +00:00
Otherwise, usage is identical to `SubscribeKey`
2019-12-15 22:22:22 +00:00
##### Subscribing to all buttons on a specific mouse
`SubscribeMouseButtons(<deviceId>, <block>, <callback>, <concurrent>)`
eg
`AHI.SubscribeMouseButtons(mouseId, true, Func("MouseButtonEvent"))`
Callback function is passed ID (See above) of pressed button and state
```
MouseButtonEvent(code, state){
ToolTip % "Mouse Button - Code: " code ", State: " state
}
```
2018-03-28 20:24:46 +00:00
#### Subscribing to Mouse Movement
**Warning!** When Subscribing to mouse movement, you will get **LOTS** of callbacks.
Note the CPU usage of the demo Monitor app.
AutoHotkey is *not good* for handling heavy processing in each callback (eg updating a GUI, like the monitor app does).
Keep your callbacks **short and efficient** in this mode if you wish to avoid high CPU usage.
##### Relative Mode
Relative mode is for normal mice and most trackpads.
2019-02-10 14:36:39 +00:00
Coordinates will be delta (change)
Each endpoint has two naming variants for convenience, they both do the same.
`SubscribeMouseMove(<deviceId>, <block>, <callback>, <concurrent>)`
`SubscribeMouseMoveRelative(<deviceId>, <block>, <callback>, <concurrent>)`
2019-02-10 14:36:39 +00:00
`UnsubscribeMouseMove(<deviceId>)`
`UnsubscribeMouseMoveRelative(<deviceId>)`
2018-03-25 16:29:55 +00:00
For Mouse Movement, the callback is passed two ints - x and y.
```
2019-08-03 12:38:12 +00:00
AHI.SubscribeMouseMove(mouseId, false, Func("MouseEvent"))
2018-03-25 16:29:55 +00:00
MouseEvent(x, y){
[...]
}
```
2018-03-25 16:38:01 +00:00
2018-03-28 20:24:46 +00:00
##### Absolute Mode
Absolute mode is used for Graphics Tablets, Light Guns etc.
Coordinates will be in the range 0..65535
`SubscribeMouseMoveAbsolute(<deviceId>, <block>, <callback>, <concurrent>)`
2019-02-10 14:36:39 +00:00
`UnsubscribeMouseMoveAbsolute(<deviceId>)`
Again, the callback is passed two ints - x and y.
2018-03-28 20:24:46 +00:00
```
2019-08-03 12:38:12 +00:00
AHI.SubscribeMouseMoveAbsolute(mouseId, false, Func("MouseEvent"))
2018-03-28 20:24:46 +00:00
MouseEvent(x, y){
[...]
}
```
## Synthesizing Output
Note that these commands will work in both Context and Subscription modes
Also note that you can send as any device, regardless of whether you have subscribed to it in some way or not.
### Sending Keyboard Keys
2018-03-12 20:46:36 +00:00
You can send keys as a specific keyboard using the `SendKeyEvent` method.
2019-08-03 12:38:12 +00:00
`AHI.SendKeyEvent(<keyboardId>, <scanCode>, <state>)`
2018-03-12 20:46:36 +00:00
scanCode = the Scan Code of the key
state = 1 for press, 0 for release
2018-03-28 20:10:29 +00:00
keyboardId = The Interception ID of the keyboard
2018-03-12 20:46:36 +00:00
```
2019-08-03 12:38:12 +00:00
AHI.SendKeyEvent(keyboardId, GetKeySC("a"), 1)
2018-03-12 20:46:36 +00:00
```
2018-03-13 00:09:44 +00:00
If you subscribe to a key using Subscription mode with the `block` parameter set to true, then send a different key using `SendKeyEvent`, you are transforming that key in a way which is totally invisible to windows (And all apps running on it), and it will respond as appropriate. For example, AHK `$` prefixed hotkeys **will not** be able to tell that this is synthetic input, and will respond to it.
2018-03-28 20:24:46 +00:00
### Sending Mouse Buttons
2018-03-31 17:53:42 +00:00
You can send clicks and other mouse button events with:
2019-08-03 12:38:12 +00:00
`AHI.SendMouseButtonEvent(<mouseId>, <button>, <state>)`
2018-03-28 22:36:07 +00:00
Where `button` is the button index, as used in `SubscribeMouseButton`
2018-03-28 20:24:46 +00:00
When Sending Mouse Wheel events, set `<state>` to `1` for Wheel Up / Right and `-1` for Wheel Down / Left.
2018-03-31 22:25:36 +00:00
2018-03-31 17:53:42 +00:00
If you are working in Absolute mode (eg with a graphics tablet or light guns), you can send mouse button events at specific coordinates using:
2019-08-03 12:38:12 +00:00
`AHI.SendMouseButtonEventAbsolute(<mouseId>, <button>, <state>, <x>, <y>)`
2018-03-31 17:53:42 +00:00
2018-03-28 20:24:46 +00:00
### Sending Mouse Movement
#### Relative
2018-03-31 17:53:42 +00:00
To send Relative (Normal) mouse movement, use:
2019-08-03 12:38:12 +00:00
`AHI.SendMouseMove(<mouseId>, <x>, <y>)`
2019-08-06 11:17:20 +00:00
X and Y are **not** setting the absolute cursor position, they are **altering current position**
Note that x and y are **not** in pixels, they are in "Mickeys"
2018-03-28 20:24:46 +00:00
#### Absolute
2018-03-31 17:53:42 +00:00
To sent Absolute mouse movement, use:
2019-08-03 12:38:12 +00:00
`AHI.SendMouseMoveAbsolute(<mouseId>, <x>, <y>)`
2018-03-31 17:53:42 +00:00
Note that Absolute mode will probably not work with FPS style mouse-aim games.
2019-08-06 11:17:20 +00:00
Note that Absolute mouse move uses coordinates in the range 0..65535 which are NOT screen coordinates. If, for example, you have one 1920x1080 monitor, then divide 65535 by 1920 to find the x position on your screen. This 65535 coordinate space maps to **all** your screens however, so if you have multiple monitors, further maths will be required.
2018-03-28 20:24:46 +00:00
2019-08-06 11:02:58 +00:00
#### Moving the Mouse Cursor
2019-08-06 11:11:54 +00:00
To move the mouse cursor to a specific screen or window coordinate, use:
2019-08-06 11:07:57 +00:00
`AHI.MoveCursor(<x>, <y> [, <coordMode>, <mouseId>])`
`coordMode` is optional and is the [CoordMode](https://www.autohotkey.com/docs/commands/CoordMode.htm) to use (Will switch back to current CoordMode after) - Defaults to "Screen".
2019-08-06 11:02:58 +00:00
`mouseId` is optional and the ID of the mouse to use (Defaults to ID 11 - the first mouse)
2019-08-06 11:23:35 +00:00
eg
`AHI.MoveCursor(100, 200)` - move to 100, 200 Screen position using mouse ID 11
`AHI.MoveCursor(100, 200, "Window")` - move to 100, 200 Window position using mouse ID 11
`AHI.MoveCursor(100, 200, , 12)` - move to 100, 200 Screen position using mouse ID 12
2020-04-29 16:40:01 +00:00
## Compiling scripts
AHI scripts can be compiled (Right click the script and select "Compile")
All required DLLs will be packed inside the EXE, so only the EXE needs to be distributed
When the EXE is run, the `Lib` folder will be created with the required DLLs