From 5960e3b01687485a4bc70e3d1da236dff958b3b6 Mon Sep 17 00:00:00 2001 From: evilC Date: Thu, 22 Mar 2018 20:42:55 +0000 Subject: [PATCH] Move to dictionary for mappings, fix for right modifier variants --- C#/AutoHotInterception/InterceptionWrapper.cs | 27 ++++++++++++------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/C#/AutoHotInterception/InterceptionWrapper.cs b/C#/AutoHotInterception/InterceptionWrapper.cs index 6773cff..0962a75 100644 --- a/C#/AutoHotInterception/InterceptionWrapper.cs +++ b/C#/AutoHotInterception/InterceptionWrapper.cs @@ -16,7 +16,7 @@ public class InterceptionWrapper private readonly bool _filterState = false; - private readonly Dictionary> _mappings = new Dictionary>(); + private readonly Dictionary> _mappings = new Dictionary>(); private readonly Dictionary _contextCallbacks = new Dictionary(); // If a the ID of a device exists as a key in this Dictionary, then that device is filtered. // Used by IsMonitoredKeyboard @@ -50,7 +50,7 @@ public class InterceptionWrapper return str; } - public bool SubscribeKey(uint code, bool block, dynamic callback, int vid = 0, int pid = 0) + public bool SubscribeKey(ushort code, bool block, dynamic callback, int vid = 0, int pid = 0) { SetFilterState(false); var id = 0; @@ -62,10 +62,10 @@ public class InterceptionWrapper if (id == 0) return false; if (!_mappings.ContainsKey(id)) { - _mappings.Add(id, new List()); + _mappings.Add(id, new Dictionary()); } - _mappings[id].Add(new Mapping() { code = Convert.ToUInt16(code), block = block, callback = callback }); + _mappings[id].Add(code, new Mapping() { block = block, callback = callback }); _filteredDevices[id] = true; SetFilterState(true); @@ -73,10 +73,10 @@ public class InterceptionWrapper return true; } - public void SendKeyEvent(int key, int state, int device = 1) + public void SendKeyEvent(ushort key, int state, int device = 1) { var stroke = new Stroke(); - stroke.key.code = (ushort)key; + stroke.key.code = key; stroke.key.state = (ushort)(1 - state); Send(_deviceContext, device, ref stroke, 1); } @@ -182,16 +182,23 @@ public class InterceptionWrapper if (isMonitoredKeyboard) { // Process Subscription Mode - foreach (var mapping in _mappings[i]) + var code = stroke.key.code; + var state = stroke.key.state; + if (state > 1) + { + code += 256; + state -= 2; + } + + if (_mappings[i].ContainsKey(code)) { - if (stroke.key.code != mapping.code) continue; hasSubscription = true; + var mapping = _mappings[i][code]; if (mapping.block) { block = true; } - mapping.callback(1 - stroke.key.state); - break; + mapping.callback(1 - state); } // If this key had no subscriptions, but Context Mode is set for this keyboard... // ... then set the Context before sending the key