diff --git a/C#/AutoHotInterception/ScanCodeChecker.cs b/C#/AutoHotInterception/ScanCodeChecker.cs index b2170b8..48f0a02 100644 --- a/C#/AutoHotInterception/ScanCodeChecker.cs +++ b/C#/AutoHotInterception/ScanCodeChecker.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.Threading; using AutoHotInterception.Helpers; namespace AutoHotInterception @@ -9,12 +10,13 @@ namespace AutoHotInterception Tool to check Scan Codes and Press / Release states Note that these are raw scancodes and states as they come from Interception. Some keys (eg extended code keys) will not match AHK key codes! */ - public class ScanCodeChecker + public class ScanCodeChecker : IDisposable { private readonly IntPtr _deviceContext; private dynamic _callback; private int _deviceId; private bool _block; + private Thread _pollThread; public ScanCodeChecker() { @@ -27,6 +29,12 @@ namespace AutoHotInterception _deviceId = deviceId; _block = block; + _pollThread = new Thread(PollThread); + _pollThread.Start(); + } + + private void PollThread() + { ManagedWrapper.SetFilter(_deviceContext, IsMonitoredDevice, ManagedWrapper.Filter.All); int deviceId1; int deviceId2; @@ -45,7 +53,7 @@ namespace AutoHotInterception strokes.Add(stroke2); } } - if (!block) + if (!_block) { for (int i = 0; i < strokes.Count; i++) { @@ -53,10 +61,12 @@ namespace AutoHotInterception ManagedWrapper.Send(_deviceContext, _deviceId, ref stroke, 1); } } - var keyEvents = new List(); - foreach (var s in strokes) + // Use array for callback, as the callback may be AHK code, and dealing with arrays in AHK is way simpler that Lists + var keyEvents = new KeyEvent[strokes.Count]; + for (int i = 0; i < strokes.Count; i++) { - keyEvents.Add(new KeyEvent { Code = s.key.code, State = s.key.state }); + var s = strokes[i]; + keyEvents[i] = new KeyEvent { Code = s.key.code, State = s.key.state }; } _callback(keyEvents); } @@ -71,7 +81,12 @@ namespace AutoHotInterception private int IsMonitoredDevice(int device) { return (Convert.ToInt32(_deviceId == device) ); - //return (Convert.ToInt32(_deviceId == device || device == 12) ); + } + + public void Dispose() + { + _pollThread.Abort(); + _pollThread.Join(); } } diff --git a/C#/TestApp/Program.cs b/C#/TestApp/Program.cs index 3f6f80b..0c4e07e 100644 --- a/C#/TestApp/Program.cs +++ b/C#/TestApp/Program.cs @@ -14,9 +14,10 @@ namespace TestApp //var kmt = new KeyboardAndMouseTester(TestDevices.WyseKeyboard, true).AddDevice(TestDevices.LogitechWheelMouse, true); //var kkt = new KeyboardKeyTester(TestDevices.WyseKeyboard, AhkKeys.Obj("1"), true); //var tt = new TabletTester(TestDevices.ParbloIslandA609); - var sct = new ScanCodeTester(TestDevices.WyseKeyboard, true); + var scc = new ScanCodeTester(TestDevices.WyseKeyboard, true); //var sst = new SetStateTester(TestDevices.WyseKeyboard, AhkKeys.Obj("1")); Console.ReadLine(); + scc.Dispose(); } } } \ No newline at end of file diff --git a/C#/TestApp/ScanCodeTester.cs b/C#/TestApp/ScanCodeTester.cs index d0644b4..a2af3af 100644 --- a/C#/TestApp/ScanCodeTester.cs +++ b/C#/TestApp/ScanCodeTester.cs @@ -5,17 +5,23 @@ using AutoHotInterception; namespace TestApp { - public class ScanCodeTester + public class ScanCodeTester : IDisposable { + private ScanCodeChecker scc; public ScanCodeTester(TestDevice device, bool block = false) { - var scc = new ScanCodeChecker(); + scc = new ScanCodeChecker(); var devId = device.GetDeviceId(); if (devId == 0) return; - scc.Subscribe(devId, new Action>(OnKeyEvent), block); + scc.Subscribe(devId, new Action(OnKeyEvent), block); } - public void OnKeyEvent(List keyEvents) + public void Dispose() + { + scc.Dispose(); + } + + public void OnKeyEvent(KeyEvent[] keyEvents) { var str = ""; foreach (var keyEvent in keyEvents) diff --git a/CHANGELOG.md b/CHANGELOG.md index a4dca6f..8a1fc53 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ then the Extended Modifier key (LShift with a state of 2 in the above example) w Similar to the above example, if you sent Home, previously, only Home would be sent with a state of 2 LShift would not have been sent with a state of 2 as it should Also, Pause should send a state of 4, whereas before it sent a state of 2 +- AhiScanCodeTester.ahk in Development Tools now works again ## [0.7.0] - 2022-01 -17 ### Added diff --git a/Development Tools/AhiScanCodeTester.ahk b/Development Tools/AhiScanCodeTester.ahk index c3c8448..2b81cff 100644 --- a/Development Tools/AhiScanCodeTester.ahk +++ b/Development Tools/AhiScanCodeTester.ahk @@ -15,15 +15,20 @@ Note that for some keys (eg Pause), AHI will see TWO key events for that key, he All ScanCodes are in Decimal */ +#include ..\Lib\AutoHotInterception.ahk + +AHI := new AutoHotInterception() vid := 0x04F2, pid := 0x0112 ; Wyse Keyboard +keyboardId := AHI.GetKeyboardId(vid, pid) OutputDebug, DBGVIEWCLEAR AhkKeyBuffer := [] asm := CLR_LoadLibrary("..\Lib\AutoHotInterception.dll") sct := asm.CreateInstance("AutoHotInterception.ScanCodeChecker") -sct.Subscribe(vid, pid, Func("AhiKeyEvent")) +sct.Subscribe(keyboardId, Func("AhiKeyEvent"), false) + ih := InputHook() ih.KeyOpt("{All}", "SN") @@ -66,7 +71,7 @@ AhiKeyEvent(keyEvents){ } ; Note that keyEvents is a ZERO-BASED array! - + ahiSc1 := keyEvents[0].Code ahiState1 := keyEvents[0].state