123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- 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;
-
- [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
- private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
-
- [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
- [return: MarshalAs(UnmanagedType.Bool)]
- private static extern bool UnhookWindowsHookEx(IntPtr hhk);
-
- [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);
-
- 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);
- }
- }
- }
|