From 0bbb79c73099e849663ff5cd37bcc1aec8cf6d0d Mon Sep 17 00:00:00 2001 From: evilC Date: Mon, 2 Jul 2018 18:54:19 +0100 Subject: [PATCH] Expose Handle, add methods to get ID from handle --- C#/AutoHotInterception/Helpers/Helpers.cs | 5 +-- C#/AutoHotInterception/Manager.cs | 41 +++++++++++++++++++++-- Lib/AutoHotInterception.ahk | 20 ++++++++++- 3 files changed, 61 insertions(+), 5 deletions(-) diff --git a/C#/AutoHotInterception/Helpers/Helpers.cs b/C#/AutoHotInterception/Helpers/Helpers.cs index 5c2ad01..1502be3 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(); @@ -91,6 +91,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 e97d7e9..5107edc 100644 --- a/C#/AutoHotInterception/Manager.cs +++ b/C#/AutoHotInterception/Manager.cs @@ -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 c745e35..252f196 100644 --- a/Lib/AutoHotInterception.ahk +++ b/Lib/AutoHotInterception.ahk @@ -82,6 +82,16 @@ 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, instance) } @@ -90,11 +100,19 @@ class AutoHotInterception { return this.GetDeviceId(true, VID, PID, instance) } + GetKeyboardIdFromHandle(handle, instance := 1){ + return this.GetDeviceId(false, handle, instance) + } + + GetMouseIDFromHandle(handle, instance := 1){ + return this.GetDeviceId(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 }