Skip to content

Commit

Permalink
Merge pull request #17 from evilC/PS2
Browse files Browse the repository at this point in the history
Fixes, PS/2 compaitibility
  • Loading branch information
evilC authored Jul 2, 2018
2 parents ecb9ae4 + f18a00f commit 1377b31
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 20 deletions.
5 changes: 3 additions & 2 deletions C#/AutoHotInterception/Helpers/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ public static DeviceInfo[] GetDeviceList(IntPtr deviceContext)
if (handle == "") continue;
int foundVid = 0, foundPid = 0;
GetVidPid(handle, ref foundVid, ref foundPid);
if (foundVid == 0 || foundPid == 0) continue;
//if (foundVid == 0 || foundPid == 0) continue;

ret.Add(new DeviceInfo { Id = i, Vid = foundVid, Pid = foundPid, IsMouse = i > 10 });
ret.Add(new DeviceInfo { Id = i, Vid = foundVid, Pid = foundPid, IsMouse = i > 10, Handle = handle});
}

return ret.ToArray();
Expand Down Expand Up @@ -87,6 +87,7 @@ public class DeviceInfo
public bool IsMouse { get; set; }
public int Vid { get; set; }
public int Pid { get; set; }
public string Handle { get; set; }
}

public class ButtonState
Expand Down
47 changes: 42 additions & 5 deletions C#/AutoHotInterception/Manager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,15 +176,15 @@ public void SetContextCallback(int id, dynamic callback)
public void SendKeyEvent(int id, ushort code, int state)
{
IsValidDeviceId(false, id);

var st = 1 - state;
var stroke = new ManagedWrapper.Stroke();
if (code > 255)
{
code -= 256;
state += 2;
st += 2;
}
stroke.key.code = code;
stroke.key.state = (ushort)(1 - state);
stroke.key.state = (ushort)st;
ManagedWrapper.Send(_deviceContext, id, ref stroke, 1);
}

Expand Down Expand Up @@ -271,6 +271,16 @@ public int GetMouseId(int vid, int pid, int instance = 1)
return GetDeviceId(true, vid, pid, instance);
}

public int GetKeyboardIdFromHandle(string handle, int instance = 1)
{
return GetDeviceIdFromHandle(false, handle, instance);
}

public int GetMouseIdFromHandle(string handle, int instance = 1)
{
return GetDeviceIdFromHandle(true, handle, instance);
}

/// <summary>
/// Tries to get Device ID from VID/PID
/// </summary>
Expand All @@ -285,9 +295,9 @@ public int GetDeviceId(bool isMouse, int vid, int pid, int instance = 1)
var max = isMouse ? 21 : 11;
for (var i = start; i < max; i++)
{
var handle = ManagedWrapper.GetHardwareStr(_deviceContext, i, 1000);
var hardwareStr = ManagedWrapper.GetHardwareStr(_deviceContext, i, 1000);
int foundVid = 0, foundPid = 0;
GetVidPid(handle, ref foundVid, ref foundPid);
GetVidPid(hardwareStr, ref foundVid, ref foundPid);
if (foundVid != vid || foundPid != pid) continue;
if (instance == 1)
{
Expand All @@ -300,6 +310,33 @@ public int GetDeviceId(bool isMouse, int vid, int pid, int instance = 1)
return 0;
}

/// <summary>
/// Tries to get Device ID from Hardware String
/// </summary>
/// <param name="isMouse">Whether the device is a mouse or a keyboard</param>
/// <param name="handle">The Hardware String (handle) of the device</param>
/// <param name="instance">The instance of the VID/PID (Optional)</param>
/// <returns></returns>
public int GetDeviceIdFromHandle(bool isMouse, string handle, int instance = 1)
{
var start = isMouse ? 11 : 0;
var max = isMouse ? 21 : 11;
for (var i = start; i < max; i++)
{
var hardwareStr = ManagedWrapper.GetHardwareStr(_deviceContext, i, 1000);
if (hardwareStr != handle) continue;

if (instance == 1)
{
return i;
}
instance--;
}

//ToDo: Should throw here?
return 0;
}

/// <summary>
/// Gets a list of connected devices
/// Intended to be used called via the AHK wrapper...
Expand Down
26 changes: 22 additions & 4 deletions Lib/AutoHotInterception.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -74,27 +74,45 @@ class AutoHotInterception {
; --------------- Querying ------------------------
GetDeviceID(IsMouse, VID, PID, instance := 1){
static devType := {0: "Keyboard", 1: "Mouse"}
dev := this.Instance.GetDeviceId(IsMouse, VID, PID)
dev := this.Instance.GetDeviceId(IsMouse, VID, PID, instance)
if (dev == 0){
MsgBox % "Could not get " devType[isMouse] " with VID " VID ", PID " PID ", Instance " instance
ExitApp
}
return dev
}

GetDeviceIdFromHandle(isMouse, handle, instance := 1){
static devType := {0: "Keyboard", 1: "Mouse"}
dev := this.Instance.GetDeviceIdFromHandle(IsMouse, handle, instance)
if (dev == 0){
MsgBox % "Could not get " devType[isMouse] " with Handle " handle ", Instance " instance
ExitApp
}
return dev
}

GetKeyboardID(VID, PID, instance := 1){
return this.GetDeviceId(false, VID, PID)
return this.GetDeviceId(false, VID, PID, instance)
}

GetMouseID(VID, PID, instance := 1){
return this.GetDeviceId(true, VID, PID)
return this.GetDeviceId(true, VID, PID, instance)
}

GetKeyboardIdFromHandle(handle, instance := 1){
return this.GetDeviceIdFromHandle(false, handle, instance)
}

GetMouseIDFromHandle(handle, instance := 1){
return this.GetDeviceIdFromHandle(true, handle, instance)
}

GetDeviceList(){
DeviceList := {}
arr := this.Instance.GetDeviceList()
for v in arr {
DeviceList[v.id] := { ID: v.id, VID: v.vid, PID: v.pid, IsMouse: v.IsMouse }
DeviceList[v.id] := { ID: v.id, VID: v.vid, PID: v.pid, IsMouse: v.IsMouse, Handle: v.Handle }
}
return DeviceList
}
Expand Down
17 changes: 9 additions & 8 deletions Monitor.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ DeviceList := MonitorWrapper.GetDeviceList()

start := 0

Gui, Add, Text, w300 Center Section, Keyboards
colWidth := 350
Gui, Add, Text, w%colWidth% Center Section, Keyboards
Loop 2 {
isMouse := A_Index - 1
Loop 10 {
Expand All @@ -27,24 +28,24 @@ Loop 2 {
if (!IsObject(dev)){
continue
}
Gui, Add, Checkbox, % "hwndhCb w300", % "ID: " dev.id ", VID: 0x" FormatHex(dev.VID) ", PID: 0x" FormatHex(dev.PID)
Gui, Add, Checkbox, % "hwndhCb w" colWidth, % "ID: " dev.id ", VID: 0x" FormatHex(dev.VID) ", PID: 0x" FormatHex(dev.PID) "`nHandle: " dev.Handle
fn := Func("CheckboxChanged").Bind(dev.id)
GuiControl, +g, % hCb, % fn
}
if (!IsMouse){
Gui, Add, Text, x+5 ym w300 Center Section, Mice
Gui, Add, Text, x+5 ym w%colWidth% Center Section, Mice
start := 10
}
}

Gui, Add, CheckBox, w300 y+20 hwndhCbFilterMove Checked, Filter Movement (Warning: Turning off can cause crashes)
Gui, Add, CheckBox, w%colWidth% y+20 hwndhCbFilterMove Checked, Filter Movement (Warning: Turning off can cause crashes)
fn := Func("FilterMove")
GuiControl, +g, % hCbFilterMove, % fn
Gui, Add, Button, xm w300 Center gClearKeyboard, Clear
Gui, Add, Button, x+5 yp w300 gClearMouse Center, Clear
Gui, Add, Button, xm w%colWidth% Center gClearKeyboard, Clear
Gui, Add, Button, x+5 yp w%colWidth% gClearMouse Center, Clear

Gui, Add, ListView, xm w300 h400 hwndhLvKeyboard, ID|State|Code|Info
Gui, Add, ListView, x+5 yp w300 h400 hwndhLvMouse, ID|State|Flags|Rolling|X|Y|Info
Gui, Add, ListView, xm w%colWidth% h400 hwndhLvKeyboard, ID|State|Code|Info
Gui, Add, ListView, x+5 yp w%colWidth% h400 hwndhLvMouse, ID|State|Flags|Rolling|X|Y|Info
LV_ModifyCol(5, 50)
LV_ModifyCol(6, 50)
Gui, Show
Expand Down
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ AHI.Instance.SendMouseMove(...)
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`).

## Finding Device IDs
### Finding a specific device
### USB Devices
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).

Expand All @@ -94,13 +94,29 @@ eg `AHI.GetDeviceId(false, 0x04F2, 0x0112)` to find a keyboard with VID 0x04F2
#### GetMouseId
`AHI.GetMouseId(<VID>, <PID> [,<instance = 1>] )`

### 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>] )`
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`

#### GetKeyboardIdFromHandle
`AHI.GetKeyboardIdFromHandle(<handle> [,<instance = 1>] )`

#### GetMouseIdFromHandle
`AHI.GetMouseIdFromHandle(<handle> [,<instance = 1>] )`

### Getting a list of devices
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:
```
Id
isMouse
Vid
Pid
Handle
```

## Input Detection
Expand Down

0 comments on commit 1377b31

Please sign in to comment.