Add SubscribeMouseButtons

feature/SubscribeAll
Clive Galway 5 years ago
parent fe5ebfa034
commit 45aade36a4

@ -26,6 +26,9 @@ namespace AutoHotInterception
private readonly ConcurrentDictionary<int, ConcurrentDictionary<ushort, MappingOptions>> _mouseButtonMappings =
new ConcurrentDictionary<int, ConcurrentDictionary<ushort, MappingOptions>>();
private readonly ConcurrentDictionary<int, MappingOptions> _mouseButtonsMappings =
new ConcurrentDictionary<int, MappingOptions>();
private readonly ConcurrentDictionary<int, MappingOptions> _mouseMoveAbsoluteMappings =
new ConcurrentDictionary<int, MappingOptions>();
@ -175,6 +178,24 @@ namespace AutoHotInterception
SetThreadState(true);
}
public void SubscribeMouseButtons(int id, bool block, dynamic callback, bool concurrent = false)
{
HelperFunctions.IsValidDeviceId(true, id);
_mouseButtonsMappings.TryAdd(id,
new MappingOptions { Block = block, Concurrent = concurrent, Callback = callback });
if (!concurrent)
{
_deviceWorkerThreads.TryAdd(id, new WorkerThread());
_deviceWorkerThreads[id].Start();
}
SetDeviceFilterState(id, true);
SetFilterState(true);
SetThreadState(true);
}
public void UnsubscribeMouseButton(int id, ushort btn)
{
HelperFunctions.IsValidDeviceId(true, id);
@ -693,25 +714,56 @@ namespace AutoHotInterception
}
var isMouseButtonsMapping = _mouseButtonsMappings.ContainsKey(i);
// Process Mouse Buttons - do this AFTER mouse movement, so that absolute mode has coordinates available at the point that the button callback is fired
if (stroke.mouse.state != 0 && _mouseButtonMappings.ContainsKey(i))
if (stroke.mouse.state != 0 && _mouseButtonMappings.ContainsKey(i) || isMouseButtonsMapping)
{
var btnStates = HelperFunctions.MouseStrokeToButtonStates(stroke);
foreach (var btnState in btnStates)
{
if (!_mouseButtonMappings[i].ContainsKey(btnState.Button)) continue;
if (!isMouseButtonsMapping && !_mouseButtonMappings[i].ContainsKey(btnState.Button)) continue;
hasSubscription = true;
var mapping = _mouseButtonMappings[i][btnState.Button];
MappingOptions mapping = null;
if (isMouseButtonsMapping)
{
mapping = _mouseButtonsMappings[i];
}
else
{
mapping = _mouseButtonMappings[i][btnState.Button];
}
var state = btnState;
if (mapping.Concurrent)
ThreadPool.QueueUserWorkItem(threadProc => mapping.Callback(state.State));
else if (_workerThreads.ContainsKey(i) &&
_workerThreads[i].ContainsKey(btnState.Button))
_workerThreads[i][btnState.Button]?.Actions
.Add(() => mapping.Callback(state.State));
{
if (isMouseButtonsMapping)
{
ThreadPool.QueueUserWorkItem(threadProc => mapping.Callback(btnState.Button, state.State));
}
else
{
ThreadPool.QueueUserWorkItem(threadProc => mapping.Callback(state.State));
}
}
else
{
if (isMouseButtonsMapping)
{
_deviceWorkerThreads[i]?.Actions
.Add(() => mapping.Callback(btnState.Button, state.State));
}
else
{
_workerThreads[i][btnState.Button]?.Actions
.Add(() => mapping.Callback(state.State));
}
}
if (mapping.Block)
{
// Remove the event for this button from the stroke, leaving other button events intact

@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoHotInterception;
namespace TestApp
{
public class MouseButtonsTester
{
public MouseButtonsTester()
{
var im = new Manager();
//var devs = im.GetDeviceList();
//var mouseHandle = @"HID\VID_046D&PID_C539&REV_3904&MI_01&Col01";
var mouseHandle = "HID\\VID_046D&PID_C00C&REV_0620"; // Logitech USB
var devId = im.GetMouseIdFromHandle(mouseHandle);
if (devId != 0)
{
im.SubscribeMouseButtons(devId, true, new Action<ushort, int>(OnButtonEvent));
}
}
public void OnButtonEvent(ushort code, int state)
{
Console.WriteLine($"Code: {code}, State: {state}");
}
}
}

@ -8,8 +8,9 @@ namespace TestApp
private static void Main()
{
//var mt = new MouseTester();
var kt = new KeyboardTester();
//var kt = new KeyboardKeyTester();
var mbt = new MouseButtonsTester();
//var kt = new KeyboardTester();
//var kkt = new KeyboardKeyTester();
//var tt = new TabletTester();
//var mon = new MonitorTester();
Console.ReadLine();

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

@ -164,6 +164,10 @@ class AutoHotInterception {
this.Instance.SubscribeMouseButton(id, btn, block, callback, concurrent)
}
SubscribeMouseButtons(id, block, callback, concurrent := false) {
this.Instance.SubscribeMouseButtons(id, block, callback, concurrent)
}
UnsubscribeMouseButton(id, btn){
this.Instance.UnsubscribeMouseButton(id, btn)
}

@ -6,10 +6,17 @@ AHI := new AutoHotInterception()
keyboardId := AHI.GetKeyboardId(0x04F2, 0x0112)
AHI.SubscribeKeyboard(keyboardId, true, Func("KeyEvent"))
mouseId := AHI.GetMouseId(0x046D, 0xC00C)
AHI.SubscribeMouseButtons(mouseId, true, Func("MouseButtonEvent"))
return
KeyEvent(code, state){
ToolTip % "Code: " code ", State: " state
ToolTip % "Keyboard Key - Code: " code ", State: " state
}
MouseButtonEvent(code, state){
ToolTip % "Mouse Button - Code: " code ", State: " state
}
^Esc::

Loading…
Cancel
Save