public static void SetFocusToDesktop() { Action d = delegate() { IntPtr winHandle = WindowsUtilities.FindWindow("CrypticWindow", null); WindowsUtilities.SetForegroundWindow(winHandle); }; System.Windows.Application.Current?.Dispatcher?.Invoke(d); }
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) { DesktopManager.SetFocusToDesktop(); } else { lock (lockObj) { try { foreach (HandleKeyEvent _handleKeyEvent in _handleKeyEvents.Where(handler => !_suspendedHandleKeyEvents.Contains(handler))) { EventMethod handler = _handleKeyEvent(vkCode, inputKey); if (handler != null) { handler(); } } } catch (Exception ex) { } } } } } } return(CallNextHook(hookID, nCode, wParam, lParam)); }
internal IntPtr HandleMouseEvent(int nCode, IntPtr wParam, IntPtr lParam) { if (nCode >= 0) { if (MouseMessage.WM_LBUTTONDOWN == (MouseMessage)wParam) { MouseClickCount++; switch (MouseClickCount) { case 1: MouseState = DesktopMouseState.LEFT_CLICK; Action action = delegate() { mouseClicksTracker.Start(); }; System.Windows.Application.Current.Dispatcher.BeginInvoke(action); break; case 2: MouseState = DesktopMouseState.DOUBLE_CLICK; break; case 3: MouseState = DesktopMouseState.TRIPLE_CLICK; break; case 4: MouseState = DesktopMouseState.QUAD_CLICK; break; } } else if (MouseMessage.WM_RBUTTONDOWN == (MouseMessage)wParam) { MouseState = DesktopMouseState.RIGHT_CLICK; } else if (MouseMessage.WM_RBUTTONUP == (MouseMessage)wParam) { MouseState = DesktopMouseState.RIGHT_CLICK_UP; } else if (MouseMessage.WM_LBUTTONUP == (MouseMessage)wParam) { MouseState = DesktopMouseState.LEFT_CLICK_UP; } else if (MouseMessage.WM_MOUSEMOVE == (MouseMessage)wParam) { MouseState = DesktopMouseState.MOUSE_MOVE; } IntPtr foregroundWindow = WindowsUtilities.GetForegroundWindow(); if (foregroundWindow == WindowsUtilities.FindWindow("CrypticWindow", null)) { if (MouseState == DesktopMouseState.MOUSE_MOVE) { FireMouseMoveEvent(); } else if (MouseState == DesktopMouseState.LEFT_CLICK) { FireMouseLeftClick(); } else if (MouseState == DesktopMouseState.RIGHT_CLICK) { FireMouseRightClick(); } else if (MouseState == DesktopMouseState.LEFT_CLICK_UP) { FireMouseLeftClickUp(); } else if (MouseState == DesktopMouseState.RIGHT_CLICK_UP) { FireMouseRightClickUp(); } else if (MouseState == DesktopMouseState.DOUBLE_CLICK) { FireMouseDoubleClick(); } else if (MouseState == DesktopMouseState.TRIPLE_CLICK) { FireMouseTripleCLick(); } } } return(MouseHook.CallNextHookEx(MouseHookID, nCode, wParam, lParam)); }