Sfoglia il codice sorgente

[魔盒]Add KeyBoardListenerManager

felix 1 anno fa
parent
commit
4aaea7f52d

+ 1 - 0
Vinno.FIS.Sonopost/Managers/AppManager.cs

@@ -111,6 +111,7 @@ namespace Vinno.FIS.Sonopost.Managers
             RegisterManager<ITestManager>(new TestManager());
             RegisterManager<IVersionCheckManager>(new VersionCheckManager());
             RegisterManager<IExternalMemoryManager>(new ExternalMemoryManager());
+            RegisterManager<IKeyBoardListenManager>(new KeyBoardListenManager());
 
             Logger.WriteLineInfo("AppManager RegisterPluginCreators Register end");
             _initialized = true;

+ 32 - 0
Vinno.FIS.Sonopost/Managers/Interfaces/IKeyBoardListenManager.cs

@@ -0,0 +1,32 @@
+using System;
+using System.Windows.Input;
+
+namespace Vinno.FIS.Sonopost.Managers.Interfaces
+{
+    internal interface IKeyBoardListenManager : ISonopostManager
+    {
+        /// <summary>
+        /// The left key set to listen
+        /// </summary>
+        Key LeftKey { get; }
+
+        /// <summary>
+        /// The right key set to listen
+        /// </summary>
+        Key RightKey { get; }
+
+        void StartKeyBoardListen(Key leftKey, Key rightKey);
+
+        void StopKeyBoardListen();
+
+        /// <summary>
+        /// Left Key Pressed Invoke
+        /// </summary>
+        event EventHandler LeftKeyPressedEvent;
+
+        /// <summary>
+        /// Right Key Pressed Invoke
+        /// </summary>
+        event EventHandler RightKeyPressedEvent;
+    }
+}

+ 136 - 0
Vinno.FIS.Sonopost/Managers/KeyBoardListenManager.cs

@@ -0,0 +1,136 @@
+using System;
+using System.Diagnostics;
+using System.Runtime.InteropServices;
+using System.Windows.Input;
+using Vinno.FIS.Sonopost.Managers.Interfaces;
+using Vinno.IUS.Common.Log;
+
+namespace Vinno.FIS.Sonopost.Managers
+{
+    internal class KeyBoardListenManager : SonopostManager, IKeyBoardListenManager
+    {
+        private const int WH_KEYBOARD_LL = 13;
+        private const int WM_KEYDOWN = 0x0100;
+        private const int WM_KEYUP = 0x0101;
+        private bool _isLeftKeyDown;
+        private bool _isRightKeyDown;
+
+        // 声明全局钩子句柄
+        private IntPtr _keyboardHookID = IntPtr.Zero;
+
+        // 键盘处理委托实例
+        private LowLevelKeyboardProc _keyboardProc;
+
+        // 导入SetWindowsHookEx函数
+        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
+        private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
+
+        // 导入UnhookWindowsHookEx函数
+        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
+        [return: MarshalAs(UnmanagedType.Bool)]
+        private static extern bool UnhookWindowsHookEx(IntPtr hhk);
+
+        // 导入CallNextHookEx函数
+        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
+        private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
+
+        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
+        private static extern IntPtr GetModuleHandle(string lpModuleName);
+
+        // 声明KeyboardProc委托类型
+        private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
+
+        public Key LeftKey { get; private set; }
+
+        public Key RightKey { get; private set; }
+
+        public event EventHandler LeftKeyPressedEvent;
+
+        public event EventHandler RightKeyPressedEvent;
+
+        public void StartKeyBoardListen(Key leftKey, Key rightKey)
+        {
+            _keyboardProc = KeyboardHookCallback;
+            _keyboardHookID = SetKeyboardHook(_keyboardProc);
+            LeftKey = leftKey;
+            RightKey = rightKey;
+        }
+
+        private IntPtr SetKeyboardHook(LowLevelKeyboardProc proc)
+        {
+            using (Process curProcess = Process.GetCurrentProcess())
+            using (ProcessModule curModule = curProcess.MainModule)
+            {
+                return SetWindowsHookEx(WH_KEYBOARD_LL, proc, GetModuleHandle(curModule.ModuleName), 0);
+            }
+        }
+
+        private void UnsetKeyboardHook()
+        {
+            UnhookWindowsHookEx(_keyboardHookID);
+        }
+
+        public void StopKeyBoardListen()
+        {
+            UnsetKeyboardHook();
+        }
+
+        public override void DoDispose()
+        {
+            try
+            {
+                StopKeyBoardListen();
+            }
+            catch (Exception ex)
+            {
+                Logger.WriteLineError($"KeyBoardListenManager DoDispose Error:{ex}");
+            }
+            base.DoDispose();
+        }
+
+        private IntPtr KeyboardHookCallback(int nCode, IntPtr wParam, IntPtr lParam)
+        {
+            try
+            {
+                if (nCode >= 0)
+                {
+                    if (wParam == (IntPtr)WM_KEYDOWN)
+                    {
+                        int vkCode = Marshal.ReadInt32(lParam);
+                        var pressedKey = KeyInterop.KeyFromVirtualKey(vkCode);
+                        if (pressedKey == LeftKey && !_isLeftKeyDown)
+                        {
+                            Logger.WriteLineWarn($"LeftKey {pressedKey} Pressed");
+                            _isLeftKeyDown = true;
+                            LeftKeyPressedEvent?.Invoke(this, EventArgs.Empty);
+                        }
+                        else if (pressedKey == RightKey && !_isRightKeyDown)
+                        {
+                            Logger.WriteLineWarn($"RightKey {pressedKey} Pressed");
+                            _isRightKeyDown = true;
+                            RightKeyPressedEvent?.Invoke(this, EventArgs.Empty);
+                        }
+                    }
+                    else if (wParam == (IntPtr)WM_KEYUP)
+                    {
+                        int vkCode = Marshal.ReadInt32(lParam);
+                        var pressedKey = KeyInterop.KeyFromVirtualKey(vkCode);
+                        if (pressedKey == LeftKey)
+                        {
+                            _isLeftKeyDown = false;
+                        }
+                        else if (pressedKey == RightKey)
+                        {
+                            _isRightKeyDown = false;
+                        }
+                    }
+                }
+            }
+            catch (Exception ex)
+            {
+                Logger.WriteLineError($"KeyboardHookCallback Error:{ex}");
+            }
+            return CallNextHookEx(_keyboardHookID, nCode, wParam, lParam);
+        }
+    }
+}

+ 2 - 0
Vinno.FIS.Sonopost/Vinno.FIS.Sonopost.csproj

@@ -79,6 +79,8 @@
     </ApplicationDefinition>
     <Compile Include="Features\Config\PageSetting.cs" />
     <Compile Include="Features\Config\ServerInfoSetting.cs" />
+    <Compile Include="Managers\Interfaces\IKeyBoardListenManager.cs" />
+    <Compile Include="Managers\KeyBoardListenManager.cs" />
     <Compile Include="WebApi\BaseController.cs" />
     <Compile Include="WebApi\Controllers\DicomController.cs" />
     <Compile Include="WebApi\Controllers\LiveController.cs" />