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));
 }
        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;
                        if (IsDesktopActive)
                        {
                            needToHandleSingleClick = true;
                        }
                        Action action = delegate()
                        {
                            DoubleTripleQuadMouseClicksTracker.Start();
                        };
                        System.Windows.Application.Current.Dispatcher.Invoke(action);
                        break;

                    case 2:
                        MouseState = DesktopMouseState.DOUBLE_CLICK;
                        needToHanldeDoubleClick = true;
                        needToHandleSingleClick = false;
                        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)
                //{
                //    if (!needToHanldeDoubleClick)
                //    {
                //        MouseState = DesktopMouseState.LEFT_CLICK_UP;
                //    }
                //}
                else if (MouseMessage.WM_MOUSEMOVE == (MouseMessage)wParam)
                {
                    MouseState = DesktopMouseState.MOUSE_MOVE;
                }
                Action d = delegate()
                {
                    IntPtr foregroundWindow = WindowsUtilities.GetForegroundWindow();
                    if (foregroundWindow == WindowsUtilities.FindWindow("CrypticWindow", null))
                    {
                        if (MouseState == DesktopMouseState.MOUSE_MOVE)
                        {
                            FireMouseMoveEvent();
                        }
                        else if (MouseState == DesktopMouseState.RIGHT_CLICK)
                        {
                            FireMouseRightClick();
                        }
                        else if (MouseState == DesktopMouseState.LEFT_CLICK)
                        {
                            if (needToHandleSingleClick)
                            {
                                FireMouseLeftClick();
                            }
                        }
                        //else if (MouseState == DesktopMouseState.LEFT_CLICK_UP)
                        //{
                        //    FireMouseLeftClickUp();
                        //}
                        else if (MouseState == DesktopMouseState.RIGHT_CLICK_UP)
                        {
                            FireMouseRightClickUp();
                        }
                        else if (MouseState == DesktopMouseState.DOUBLE_CLICK)
                        {
                            if (needToHanldeDoubleClick)
                            {
                                FireMouseDoubleClick();
                            }
                        }
                    }
                };

                AsyncDelegateExecuter adex = new AsyncDelegateExecuter(d, 50);
                adex.ExecuteAsyncDelegate();
            }
            return(MouseHook.CallNextHookEx(mouseHookID, nCode, wParam, lParam));
        }