示例#1
0
        /// <summary>
        /// 註冊Windows Hook時用到的委派方法,當全域事件發生時會執行這個方法,並提供全域事件資料。
        /// </summary>
        private static int HookProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            MouseEventArgs e = null;

            if (nCode >= 0)
            {
                int wParam_Int32 = wParam.ToInt32();
                NativeStructs.MOUSELLHookStruct mouseHookStruct = (NativeStructs.MOUSELLHookStruct)Marshal.PtrToStructure(lParam, typeof(NativeStructs.MOUSELLHookStruct));

                short mouseDelta = 0;

                if (GlobalMouseWheel != null && wParam_Int32 == NativeContansts.WM_MOUSEWHEEL)
                {
                    mouseDelta = (short)((mouseHookStruct.MouseData >> 16) & 0xffff);
                }

                e = new MouseEventArgs(wParam_Int32, mouseHookStruct.Point.X, mouseHookStruct.Point.Y, mouseDelta);

                if (GlobalMouseWheel != null && wParam_Int32 == NativeContansts.WM_MOUSEWHEEL)
                {
                    GlobalMouseWheel.Invoke(null, e);
                }
                else if (GlobalMouseUp != null && (wParam_Int32 == NativeContansts.WM_LBUTTONUP || wParam_Int32 == NativeContansts.WM_RBUTTONUP || wParam_Int32 == NativeContansts.WM_MBUTTONUP))
                {
                    GlobalMouseUp.Invoke(null, e);
                    if (GlobalMouseClick != null && (mouseHookStruct.Point.X == m_LastBTDownX && mouseHookStruct.Point.Y == m_LastBTDownY))
                    {
                        GlobalMouseClick.Invoke(null, e);
                    }
                }
                else if (GlobalMouseDown != null && (wParam_Int32 == NativeContansts.WM_LBUTTONDOWN || wParam_Int32 == NativeContansts.WM_RBUTTONDOWN || wParam_Int32 == NativeContansts.WM_MBUTTONDOWN))
                {
                    m_LastBTDownX = mouseHookStruct.Point.X;
                    m_LastBTDownY = mouseHookStruct.Point.Y;
                    GlobalMouseDown.Invoke(null, e);
                }
                else if (GlobalMouseMove != null && (m_OldX != mouseHookStruct.Point.X || m_OldY != mouseHookStruct.Point.Y))
                {
                    m_OldX = mouseHookStruct.Point.X;
                    m_OldY = mouseHookStruct.Point.Y;
                    if (GlobalMouseMove != null)
                    {
                        GlobalMouseMove.Invoke(null, e);
                    }
                }
            }

            if (Monopolize || (e != null && e.Handled))
            {
                return(-1);
            }

            return(NativeMethods.CallNextHookEx(m_HookHandle, nCode, wParam, lParam));
        }
示例#2
0
        protected virtual void OnGlobalMouseDown(GLBaseControl ctrl, GLMouseEventArgs e) // everyone gets this
        {
            //System.Diagnostics.Debug.WriteLine("In " + Name + " Global click in " + ctrl.Name);
            GlobalMouseDown?.Invoke(ctrl, e);
            List <GLBaseControl> list = new List <GLBaseControl>(childrenz); // copy of, in case the caller closes something

            foreach (var c in list)
            {
                c.OnGlobalMouseDown(ctrl, e);
            }
        }
示例#3
0
        private void InitMouseEvents()
        {
            // TODO: mouse wheel

            var mouse = _inputContext.Mice.FirstOrDefault();

            if (mouse != null)
            {
                mouse.MouseMove += (mouse, point) =>
                {
                    RunMouseHandlers((c, args) => c.OnMouseMove(args),
                                     new MouseMoveEventArgs(point.X, point.Y, _currentMouseButtons)
                                     );
                    GlobalMouseMove?.Invoke(mouse.Position.Approximate());
                };
                mouse.MouseDown += (mouse, button) =>
                {
                    _lastMouseDownPositions[button] = mouse.Position;
                    RunMouseHandlers((c, args) => c.OnMouseDown(args),
                                     new MouseButtonEventArgs(mouse.Position.X, mouse.Position.Y, button, _currentModifiers),
                                     (c, args) => c.OnMouseDownOutside(args)
                                     );
                    GlobalMouseDown?.Invoke(mouse.Position.Approximate());
                };
                mouse.MouseUp += (mouse, button) =>
                {
                    RunMouseHandlers((c, args) => c.OnMouseUp(args),
                                     new MouseButtonEventArgs(mouse.Position.X, mouse.Position.Y, button, _currentModifiers),
                                     (c, args) => c.OnMouseUpOutside(args)
                                     );
                    GlobalMouseUp?.Invoke(mouse.Position.Approximate());
                };
                mouse.Click += (mouse, button) =>
                               RunMouseHandlers((c, args) => c.OnMouseClick(args),
                                                new MouseButtonEventArgs(_lastMouseDownPositions[button].X,
                                                                         _lastMouseDownPositions[button].Y, button, _currentModifiers)
                                                );
                mouse.DoubleClick += (mouse, button) =>
                                     RunMouseHandlers((c, args) => c.OnMouseDoubleClick(args),
                                                      new MouseButtonEventArgs(_lastMouseDownPositions[button].X,
                                                                               _lastMouseDownPositions[button].Y, button, _currentModifiers)
                                                      );
            }
        }
示例#4
0
        public bool PreFilterMessage(ref Message m)
        {
            const int WM_MOUSEMOVE   = 0x0200;
            const int WM_LBUTTONDOWN = 0x0201;
            const int WM_MBUTTONDOWN = 0x0207;
            const int WM_RBUTTONDOWN = 0x0204;

            switch (m.Msg)
            {
            case WM_MOUSEMOVE:
                GlobalMouseMove?.Invoke(this, EventArgs.Empty);
                break;

            case WM_LBUTTONDOWN:
            case WM_MBUTTONDOWN:
            case WM_RBUTTONDOWN:
                GlobalMouseDown?.Invoke(this, new MouseEventArgs(
                                            toMouseButtons(m.Msg),
                                            0,
                                            m.LParam.LowWord(),
                                            m.LParam.HighWord(),
                                            0));
                break;
            }

            return(false);

            MouseButtons toMouseButtons(int msg)
            {
                switch (msg)
                {
                case WM_LBUTTONDOWN:
                    return(MouseButtons.Left);

                case WM_MBUTTONDOWN:
                    return(MouseButtons.Middle);

                case WM_RBUTTONDOWN:
                    return(MouseButtons.Right);
                }

                throw new ArgumentException();
            }
        }
        /// <summary>
        /// A callback function which will be called every time a mouse activity is detected.
        /// </summary>
        /// <param name="nCode">Specifies whether the hook procedure must process the message.</param>
        /// <param name="wParam">Specifies whether the message was sent by the current thread.</param>
        /// <param name="lParam">A pointer to a CWPSTRUCT structure that contains details about the message.</param>
        /// <returns></returns>
        /// <remarks>http://msdn.microsoft.com/en-us/library/windows/desktop/ms644975(v=vs.85).aspx</remarks>
        private int MouseHookProc(int nCode, int wParam, IntPtr lParam)
        {
            if (nCode >= 0)
            {
                // Marshal the data from callback.
                MSLLHOOKSTRUCT mouseHookStruct = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));

                // Detect mouse button used.
                MouseButton button = MouseButton.Middle;
                if (wParam == WM_RBUTTONDBLCLK || wParam == WM_RBUTTONDOWN || wParam == WM_RBUTTONUP)
                {
                    button = MouseButton.Right;
                }
                else if (wParam == WM_LBUTTONDBLCLK || wParam == WM_LBUTTONDOWN || wParam == WM_LBUTTONUP)
                {
                    button = MouseButton.Left;
                }

                // Generate event.
                var mouseEventArgs = new GlobalMouseEventHandlerArgs(
                    new System.Windows.Point(mouseHookStruct.pt.x, mouseHookStruct.pt.y),
                    button,
                    mouseHookStruct.mouseData,
                    mouseHookStruct.flags,
                    mouseHookStruct.time,
                    mouseHookStruct.dwExtraInfo,
                    GetHighOrderWord(mouseHookStruct.mouseData));

                // Mouse button up.
                if (GlobalMouseUp != null && (wParam == WM_RBUTTONUP || wParam == WM_LBUTTONUP))
                {
                    GlobalMouseUp.Invoke(null, mouseEventArgs);
                }

                // Mouse button down.
                if (GlobalMouseDown != null && (wParam == WM_RBUTTONDOWN || wParam == WM_LBUTTONDOWN))
                {
                    GlobalMouseDown.Invoke(null, mouseEventArgs);
                }

                // The mouse wheel was moved.
                if (GlobalMouseWheel != null && wParam == WM_MOUSEWHEEL)
                {
                    GlobalMouseWheel.Invoke(null, mouseEventArgs);
                }

                // If there is a listener for mouse movement and a mouse movement occured.
                if ((GlobalMouseMove != null) && wParam == WM_MOUSEMOVE)
                {
                    GlobalMouseMove.Invoke(null, mouseEventArgs);
                }

                // Exit method here without calling next hook if is event was handled.
                if (mouseEventArgs.Handled)
                {
                    return(-1);
                }
            }

            // Call next hook.
            return(CallNextHookEx(windowsMouseHookHandle, nCode, wParam, lParam));
        }
示例#6
0
        /// <summary>
        /// A callback function which will be called every Time a mouse activity detected.
        /// </summary>
        /// <param name="nCode">
        /// [in] Specifies whether the hook procedure must process the message.
        /// If nCode is HC_ACTION, the hook procedure must process the message.
        /// If nCode is less than zero, the hook procedure must pass the message to the
        /// CallNextHookEx function without further processing and must return the
        /// value returned by CallNextHookEx.
        /// </param>
        /// <param name="wParam">
        /// [in] Specifies whether the message was sent by the current thread.
        /// If the message was sent by the current thread, it is nonzero; otherwise, it is zero.
        /// </param>
        /// <param name="lParam">
        /// [in] Pointer to a CWPSTRUCT structure that contains details about the message.
        /// </param>
        /// <returns>
        /// If nCode is less than zero, the hook procedure must return the value returned by CallNextHookEx.
        /// If nCode is greater than or equal to zero, it is highly recommended that you call CallNextHookEx
        /// and return the value it returns; otherwise, other applications that have installed WH_CALLWNDPROC
        /// hooks will not receive hook notifications and may behave incorrectly as a result. If the hook
        /// procedure does not call CallNextHookEx, the return value should be zero.
        /// </returns>
        private int MouseHookProc(int nCode, int wParam, IntPtr lParam)
        {
            if (nCode >= 0)
            {
                //Marshall the data from callback.
                var mouseHookStruct = (MouseLLHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseLLHookStruct));


                //detect button clicked
                //MouseHookEventArgs mouseArgs = new MouseHookEventArgs();
                short mouseDelta = 0;
                var   clickCount = 0;
                var   mouseDown  = false;
                var   mouseUp    = false;

                switch (wParam)
                {
                case WM_LBUTTONDOWN:
                    mouseDown  = true;
                    clickCount = 1;
                    break;

                case WM_LBUTTONUP:
                    mouseUp    = true;
                    clickCount = 1;
                    break;

                case WM_LBUTTONDBLCLK:
                    clickCount = 2;
                    break;

                case WM_RBUTTONDOWN:
                    mouseDown  = true;
                    clickCount = 1;
                    break;

                case WM_RBUTTONUP:
                    mouseUp    = true;
                    clickCount = 1;
                    break;

                case WM_RBUTTONDBLCLK:
                    clickCount = 2;
                    break;

                case WM_MOUSEWHEEL:
                    //If the message is WM_MOUSEWHEEL, the high-order word of MouseData member is the wheel delta.
                    //One wheel click is defined as WHEEL_DELTA, which is 120.
                    //(value >> 16) & 0xffff; retrieves the high-order word from the given 32-bit value
                    mouseDelta = (short)((mouseHookStruct.MouseData >> 16) & 0xffff);

                    //TODO: X BUTTONS (I havent them so was unable to test)
                    //If the message is WM_XBUTTONDOWN, WM_XBUTTONUP, WM_XBUTTONDBLCLK, WM_NCXBUTTONDOWN, WM_NCXBUTTONUP,
                    //or WM_NCXBUTTONDBLCLK, the high-order word specifies which X button was pressed or released,
                    //and the low-order word is reserved. This value can be one or more of the following values.
                    //Otherwise, MouseData is not used.
                    break;
                }

                //generate event
                var mouseEventArgs =
                    new GlobalMouseEventHandlerArgs(
                        new System.Windows.Point(mouseHookStruct.Point.X, mouseHookStruct.Point.Y),
                        mouseHookStruct.MouseData, mouseHookStruct.Flags, mouseHookStruct.Time,
                        mouseHookStruct.ExtraInfo);

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

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

                //If someone listens to click and a click occured
                if (GlobalMouseClick != null && clickCount > 0)
                {
                    GlobalMouseClick.Invoke(null, mouseEventArgs);
                }

                //If someone listens to double click and a click occured
                if (GlobalMouseDoubleClick != null && clickCount == 2)
                {
                    GlobalMouseDoubleClick.Invoke(null, mouseEventArgs);
                }

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

                //If someone listens to move and there was a change in coordinates raise move event
                if ((GlobalMouseMove != null) && (_oldMouseX != mouseHookStruct.Point.X || _oldMouseY != mouseHookStruct.Point.Y))
                {
                    _oldMouseX = mouseHookStruct.Point.X;
                    _oldMouseY = mouseHookStruct.Point.Y;
                    if (GlobalMouseMove != null)
                    {
                        GlobalMouseMove.Invoke(null, mouseEventArgs);
                    }
                }

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

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