private void MouseClickEventHook(object sender, MouseEventExtArgs e)
        {
            Debug.WriteLine("MouseClickEventHook");
            System.Windows.Forms.MouseButtons clickedButton = e.Button;
            MouseClickType clickedType = (MouseClickType)e.Clicks;

            this.ModelItem.Properties["MouseButton"].SetValue(clickedButton);
            this.ModelItem.Properties["MouseClickAction"].SetValue(clickedType);

            if (MouseHook.Element != null)
            {
                SetElementInfo();
            }
        }
            private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
            {
                int nClickCount = 0;

                //if (nCode >= 0 && (MouseMessages.WM_LBUTTONDOWN == (MouseMessages)wParam || MouseMessages.WM_RBUTTONDOWN == (MouseMessages)wParam))
                if (nCode >= 0 && (MouseMessages.WM_LBUTTONDOWN == (MouseMessages)wParam ||
                                   MouseMessages.WM_RBUTTONDOWN == (MouseMessages)wParam)
                    //|| MouseMessages.WM_MOUSEMOVE == (MouseMessages)wParam)
                    )
                {
                    MouseLLHookStruct mouseHookStruct = (MouseLLHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseLLHookStruct));
                    leastPoint.X = mouseHookStruct.Point.X;
                    leastPoint.Y = mouseHookStruct.Point.Y;

                    if (MouseMessages.WM_LBUTTONDOWN == (MouseMessages)wParam)
                    {
                        leastClickedButton = System.Windows.Forms.MouseButtons.Left;
                    }

                    if (MouseMessages.WM_RBUTTONDOWN == (MouseMessages)wParam)
                    {
                        leastClickedButton = System.Windows.Forms.MouseButtons.Right;
                    }

                    MSLLHOOKSTRUCT hookStruct = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));

                    short mouseDelta = 0;

                    if (isFirstClick)
                    {
                        isFirstClick  = false;
                        isDoubleClick = false;
                        nClickCount   = 1;
                        doubleClickTimer.Start();
                    }
                    else
                    {
                        if (milliseconds <= System.Windows.Forms.SystemInformation.DoubleClickTime)
                        {
                            isDoubleClick = true;
                            nClickCount   = 2;

                            doubleClickTimer.Stop();
                        }
                        isFirstClick = true;
                    }

                    MouseEventExtArgs mouseArgs = new MouseEventExtArgs(leastClickedButton,
                                                                        nClickCount,  //single CLICK
                                                                        leastPoint.X,
                                                                        leastPoint.Y,
                                                                        0);

                    if (nClickCount == 1) //single click
                    {
                        //별도 쓰레드로 분리 EventHook과 UIAutomation 동일 Thread 사용시 오류 발생
                        //var tasks = Task.Factory.StartNew(GetElement);
                        var tasks = Task.Factory.StartNew(() => GetElementInfo(leastPoint.X, leastPoint.Y));
                        Task.WaitAll(tasks);
                    }


                    if (isDoubleClick)
                    {
                        Debug.WriteLine("Hook Double Clicked");
                        MouseDblClicked(null, mouseArgs);
                    }
                    else
                    {
                        Debug.WriteLine("Hook Single Clicked");

                        MouseClicked(null, mouseArgs);
                    }
                }

                return(CallNextHookEx(_hookID, nCode, wParam, lParam));
            }
Esempio n. 3
0
        private static int MouseHookProc(int nCode, int wParam, IntPtr lParam)
        {
            if (nCode >= 0)
            {
                //Marshall the data from callback.
                MouseLLHookStruct mouseHookStruct = (MouseLLHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseLLHookStruct));

                //detect button clicked
                MouseButtons button     = MouseButtons.None;
                short        mouseDelta = 0;
                int          clickCount = 0;
                bool         mouseDown  = false;
                bool         mouseUp    = false;

                switch (wParam)
                {
                case WM_LBUTTONDOWN:
                    mouseDown  = true;
                    button     = MouseButtons.Left;
                    clickCount = 1;
                    break;

                case WM_LBUTTONUP:
                    mouseUp    = true;
                    button     = MouseButtons.Left;
                    clickCount = 1;
                    break;

                case WM_LBUTTONDBLCLK:
                    button     = MouseButtons.Left;
                    clickCount = 2;
                    break;

                case WM_RBUTTONDOWN:
                    mouseDown  = true;
                    button     = MouseButtons.Right;
                    clickCount = 1;
                    break;

                case WM_RBUTTONUP:
                    mouseUp    = true;
                    button     = MouseButtons.Right;
                    clickCount = 1;
                    break;

                case WM_RBUTTONDBLCLK:
                    button     = MouseButtons.Right;
                    clickCount = 2;
                    break;

                case WM_MOUSEWHEEL:
                    mouseDelta = (short)((mouseHookStruct.MouseData >> 16) & 0xffff);

                    break;
                }

                //generate event
                MouseEventExtArgs e = new MouseEventExtArgs(
                    button,
                    clickCount,
                    mouseHookStruct.Point.X,
                    mouseHookStruct.Point.Y,
                    mouseDelta);

                //Mouse up
                if (s_MouseUp != null && mouseUp)
                {
                    s_MouseUp.Invoke(null, e);
                }

                //Mouse down
                if (s_MouseDown != null && mouseDown)
                {
                    s_MouseDown.Invoke(null, e);
                }

                //If someone listens to click and a click is heppened
                if (s_MouseClick != null && clickCount > 0)
                {
                    s_MouseClick.Invoke(null, e);
                }

                //If someone listens to click and a click is heppened
                if (s_MouseClickExt != null && clickCount > 0)
                {
                    s_MouseClickExt.Invoke(null, e);
                }

                //If someone listens to double click and a click is heppened
                if (s_MouseDoubleClick != null && clickCount == 2)
                {
                    s_MouseDoubleClick.Invoke(null, e);
                }

                //Wheel was moved
                if (s_MouseWheel != null && mouseDelta != 0)
                {
                    s_MouseWheel.Invoke(null, e);
                }

                //If someone listens to move and there was a change in coordinates raise move event
                if ((s_MouseMove != null || s_MouseMoveExt != null) && (m_OldX != mouseHookStruct.Point.X || m_OldY != mouseHookStruct.Point.Y))
                {
                    m_OldX = mouseHookStruct.Point.X;
                    m_OldY = mouseHookStruct.Point.Y;
                    if (s_MouseMove != null)
                    {
                        s_MouseMove.Invoke(null, e);
                    }

                    if (s_MouseMoveExt != null)
                    {
                        s_MouseMoveExt.Invoke(null, e);
                    }
                }

                if (e.Handled)
                {
                    return(-1);
                }
            }

            //call next hook
            return(CallNextHookEx(s_MouseHookHandle, nCode, wParam, lParam));
        }