diff --git a/C#/AutoHotInterception/Helpers/Helpers.cs b/C#/AutoHotInterception/Helpers/Helpers.cs index db08e55..ac7cd53 100644 --- a/C#/AutoHotInterception/Helpers/Helpers.cs +++ b/C#/AutoHotInterception/Helpers/Helpers.cs @@ -36,9 +36,9 @@ namespace AutoHotInterception.Helpers 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(); @@ -87,6 +87,7 @@ namespace AutoHotInterception.Helpers public bool IsMouse { get; set; } public int Vid { get; set; } public int Pid { get; set; } + public string Handle { get; set; } } public class ButtonState diff --git a/C#/AutoHotInterception/Manager.cs b/C#/AutoHotInterception/Manager.cs index 9b9b6d4..8198f9b 100644 --- a/C#/AutoHotInterception/Manager.cs +++ b/C#/AutoHotInterception/Manager.cs @@ -176,15 +176,15 @@ namespace AutoHotInterception 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); } @@ -271,6 +271,16 @@ namespace AutoHotInterception 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); + } + /// /// Tries to get Device ID from VID/PID /// @@ -285,9 +295,9 @@ namespace AutoHotInterception 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) { @@ -300,6 +310,33 @@ namespace AutoHotInterception return 0; } + /// + /// Tries to get Device ID from Hardware String + /// + /// Whether the device is a mouse or a keyboard + /// The Hardware String (handle) of the device + /// The instance of the VID/PID (Optional) + /// + 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; + } + /// /// Gets a list of connected devices /// Intended to be used called via the AHK wrapper... diff --git a/Lib/AutoHotInterception.ahk b/Lib/AutoHotInterception.ahk index f8db5ae..6394430 100644 --- a/Lib/AutoHotInterception.ahk +++ b/Lib/AutoHotInterception.ahk @@ -74,7 +74,7 @@ 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 @@ -82,19 +82,37 @@ class AutoHotInterception { 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 } diff --git a/Monitor.ahk b/Monitor.ahk index 63871e4..116e949 100644 --- a/Monitor.ahk +++ b/Monitor.ahk @@ -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 { @@ -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 diff --git a/README.md b/README.md index 6b84a10..6b88570 100644 --- a/README.md +++ b/README.md @@ -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). @@ -94,6 +94,21 @@ eg `AHI.GetDeviceId(false, 0x04F2, 0x0112)` to find a keyboard with VID 0x04F2 #### GetMouseId `AHI.GetMouseId(, [,] )` +### 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(, [,] )` +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( [,] )` + +#### GetMouseIdFromHandle +`AHI.GetMouseIdFromHandle( [,] )` + ### 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: ``` @@ -101,6 +116,7 @@ Id isMouse Vid Pid +Handle ``` ## Input Detection