internal IntPtr HandleKeyboardEvent(int nCode, IntPtr wParam, IntPtr lParam) { if (nCode >= 0) { KBDLLHOOKSTRUCT keyboardLLHookStruct = (KBDLLHOOKSTRUCT)(Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT))); this.vkCode = (Keys)keyboardLLHookStruct.vkCode; KeyboardMessage wmKeyboard = (KeyboardMessage)wParam; if ((wmKeyboard == KeyboardMessage.WM_KEYDOWN || wmKeyboard == KeyboardMessage.WM_SYSKEYDOWN)) { IntPtr foregroundWindow = WindowsUtilities.GetForegroundWindow(); uint wndProcId; uint wndProcThread = WindowsUtilities.GetWindowThreadProcessId(foregroundWindow, out wndProcId); if (foregroundWindow == WindowsUtilities.FindWindow("CrypticWindow", null) || Process.GetCurrentProcess().Id == wndProcId) { System.Windows.Input.Key inputKey = InputKey; if ((inputKey == Key.Left || inputKey == Key.Right) && Keyboard.Modifiers == ModifierKeys.Control) { IntPtr winHandle = WindowsUtilities.FindWindow("CrypticWindow", null); WindowsUtilities.SetForegroundWindow(winHandle); } else { lock (lockObj) { try { foreach (HandleKeyEvent _handleKeyEvent in _handleKeyEvents) { EventMethod handler = _handleKeyEvent(vkCode, inputKey); if (handler != null) { handler(); } } } catch (Exception ex) { } } } } } } return(CallNextHook(hookID, nCode, wParam, lParam)); }