Move Device Querying functions into HelperFunctions, add MonitorTester

hotfix/issue-44
Clive Galway 5 years ago
parent cb8c4af907
commit 8acb96ddc6

@ -23,6 +23,65 @@ namespace AutoHotInterception.Helpers
pid = Convert.ToInt32(matches[0].Groups[2].Value, 16);
}
#region Device Querying
/// <summary>
/// Tries to get Device ID from VID/PID
/// </summary>
/// <param name="deviceContext">The Interception device context</param>
/// <param name="isMouse">Whether the device is a mouse or a keyboard</param>
/// <param name="vid">The VID of the device</param>
/// <param name="pid">The PID of the device</param>
/// <param name="instance">The instance of the VID/PID (Optional)</param>
/// <returns></returns>
public static int GetDeviceId(IntPtr deviceContext, bool isMouse, int vid, int pid, 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);
int foundVid = 0, foundPid = 0;
GetVidPid(hardwareStr, ref foundVid, ref foundPid);
if (foundVid != vid || foundPid != pid) continue;
if (instance == 1) return i;
instance--;
}
//ToDo: Should throw here?
return 0;
}
/// <summary>
/// Tries to get Device ID from Hardware String
/// </summary>
/// <param name="deviceContext">The Interception device context</param>
/// <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 static int GetDeviceIdFromHandle(IntPtr deviceContext, 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...
/// ... so it can convert the return value into an AHK array
/// </summary>
/// <returns></returns>
public static DeviceInfo[] GetDeviceList(IntPtr deviceContext)
{
var ret = new List<DeviceInfo>();
@ -39,6 +98,7 @@ namespace AutoHotInterception.Helpers
return ret.ToArray();
}
#endregion
/// <summary>
/// Converts a button index plus a state into a State value for a mouse Stroke

@ -397,62 +397,17 @@ namespace AutoHotInterception
public int GetKeyboardIdFromHandle(string handle, int instance = 1)
{
return GetDeviceIdFromHandle(false, handle, instance);
return GetDeviceIdFromHandle(_deviceContext, false, handle, instance);
}
public int GetMouseIdFromHandle(string handle, int instance = 1)
{
return GetDeviceIdFromHandle(true, handle, instance);
return GetDeviceIdFromHandle(_deviceContext, true, handle, instance);
}
/// <summary>
/// Tries to get Device ID from VID/PID
/// </summary>
/// <param name="isMouse">Whether the device is a mouse or a keyboard</param>
/// <param name="vid">The VID of the device</param>
/// <param name="pid">The PID of the device</param>
/// <param name="instance">The instance of the VID/PID (Optional)</param>
/// <returns></returns>
public int GetDeviceId(bool isMouse, int vid, int pid, 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);
int foundVid = 0, foundPid = 0;
GetVidPid(hardwareStr, ref foundVid, ref foundPid);
if (foundVid != vid || foundPid != pid) continue;
if (instance == 1) return i;
instance--;
}
//ToDo: Should throw here?
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;
return HelperFunctions.GetDeviceId(_deviceContext, isMouse, vid, pid, instance);
}
/// <summary>

@ -60,6 +60,31 @@ namespace AutoHotInterception
return HelperFunctions.GetDeviceList(_deviceContext);
}
public int GetKeyboardId(int vid, int pid, int instance = 1)
{
return GetDeviceId(false, vid, pid, instance);
}
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(_deviceContext, false, handle, instance);
}
public int GetMouseIdFromHandle(string handle, int instance = 1)
{
return GetDeviceIdFromHandle(_deviceContext, true, handle, instance);
}
public int GetDeviceId(bool isMouse, int vid, int pid, int instance = 1)
{
return HelperFunctions.GetDeviceId(_deviceContext, isMouse, vid, pid, instance);
}
#endregion
#region Private

@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoHotInterception;
namespace TestApp
{
public class MonitorTester
{
public MonitorTester()
{
var mon = new Monitor();
mon.Subscribe(new Action<int, int, int, string>((id, code, value, info) =>
{
Console.WriteLine($"Keyboard: ID={id}, Code={code}, Value={value}, Info={info}");
}),
new Action<int, int, int, string>((id, code, value, info) =>
{
Console.WriteLine($"Mouse: ID={id}, Code={code}, Value={value}, Info={info}");
})
);
var devId = mon.GetKeyboardId(0x04F2, 0x0112);
mon.SetDeviceFilterState(devId, true);
}
}
}

@ -10,6 +10,7 @@ namespace TestApp
var mt = new MouseTester();
//var kt = new KeyboardTester();
//var tt = new TabletTester();
var mon = new MonitorTester();
Console.ReadLine();
}
}

@ -43,6 +43,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="KeyboardTester.cs" />
<Compile Include="MonitorTester.cs" />
<Compile Include="MouseTester.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />

Loading…
Cancel
Save