From 6ed28dc69c20b2bf81f6a7cdb8ab8f12c9ab3fc4 Mon Sep 17 00:00:00 2001 From: Clive Galway Date: Sat, 15 Jan 2022 16:39:54 +0000 Subject: [PATCH] Add UnsubscribeKey --- .../DeviceHandlers/KeyboardHandler.cs | 34 +++++++++++++------ C#/AutoHotInterception/Manager.cs | 30 ++-------------- 2 files changed, 26 insertions(+), 38 deletions(-) diff --git a/C#/AutoHotInterception/DeviceHandlers/KeyboardHandler.cs b/C#/AutoHotInterception/DeviceHandlers/KeyboardHandler.cs index 86fe487..67a077c 100644 --- a/C#/AutoHotInterception/DeviceHandlers/KeyboardHandler.cs +++ b/C#/AutoHotInterception/DeviceHandlers/KeyboardHandler.cs @@ -8,12 +8,36 @@ namespace AutoHotInterception.DeviceHandlers class KeyboardHandler : DeviceHandler { private ConcurrentDictionary KeyboardKeyMappings = new ConcurrentDictionary(); + private MappingOptions KeyboardMappings; public KeyboardHandler(IntPtr deviceContext, int deviceId) : base (deviceContext, deviceId) { } + public void SubscribeKey(ushort code, MappingOptions mappingOptions) + { + KeyboardKeyMappings.TryAdd(code, mappingOptions); + if (!mappingOptions.Concurrent) + { + WorkerThreads.TryAdd(code, new WorkerThread()); + WorkerThreads[code].Start(); + } + } + + public void UnsubscribeKey(ushort code) + { + KeyboardKeyMappings.TryRemove(code, out _); + if (KeyboardKeyMappings.Count == 0) + { + // Don't remove filter if all keys subscribed + if (KeyboardMappings != null) + { + IsFiltered = false; + } + } + } + public override void ProcessStroke(ManagedWrapper.Stroke stroke) { //ManagedWrapper.Send(DeviceContext, _deviceId, ref stroke, 1); @@ -93,15 +117,5 @@ namespace AutoHotInterception.DeviceHandlers //if (!hasSubscription && hasContext) ContextCallbacks[i](0); } } - - public void SubscribeKey(ushort code, MappingOptions mappingOptions) - { - KeyboardKeyMappings.TryAdd(code, mappingOptions); - if (!mappingOptions.Concurrent) - { - WorkerThreads.TryAdd(code, new WorkerThread()); - WorkerThreads[code].Start(); - } - } } } diff --git a/C#/AutoHotInterception/Manager.cs b/C#/AutoHotInterception/Manager.cs index 38d2e09..68b6dc6 100644 --- a/C#/AutoHotInterception/Manager.cs +++ b/C#/AutoHotInterception/Manager.cs @@ -104,21 +104,6 @@ namespace AutoHotInterception var handler = (KeyboardHandler)DeviceHandlers[id]; handler.SubscribeKey(code, new MappingOptions { Block = block, Concurrent = concurrent, Callback = callback }); - //if (!KeyboardKeyMappings.ContainsKey(id)) - // KeyboardKeyMappings.TryAdd(id, new ConcurrentDictionary()); - - //KeyboardKeyMappings[id].TryAdd(code, - // new MappingOptions { Block = block, Concurrent = concurrent, Callback = callback }); - - //if (!concurrent) - //{ - // if (!WorkerThreads.ContainsKey(id)) - // WorkerThreads.TryAdd(id, new ConcurrentDictionary()); - - // WorkerThreads[id].TryAdd(code, new WorkerThread()); - // WorkerThreads[id][code].Start(); - //} - SetDeviceFilterState(id, true); SetFilterState(true); SetThreadState(true); @@ -129,19 +114,8 @@ namespace AutoHotInterception HelperFunctions.IsValidDeviceId(false, id); SetFilterState(false); - if (KeyboardKeyMappings.TryGetValue(id, out var thisDevice)) - { - thisDevice.TryRemove(code, out _); - if (thisDevice.Count == 0) - { - KeyboardKeyMappings.TryRemove(id, out _); - // Don't remove filter if all keys subscribed - if (!KeyboardMappings.ContainsKey(id)) - { - SetDeviceFilterState(id, false); - } - } - } + var handler = (KeyboardHandler)DeviceHandlers[id]; + handler.UnsubscribeKey(code); SetFilterState(true); SetThreadState(true);