예제 #1
0
        /// <summary>
        /// This method is designed to monitor mouse clicks in order to fire a double click event if interval between
        /// clicks was short enaugh.
        /// </summary>
        /// <param name="sender">Is always null</param>
        /// <param name="e">Some information about click heppened.</param>
        private void OnMouseUpExtended(object sender, MouseEventExtendedArgs e)
        {
            if (e.Clicks > 0)
            {
                // If the second click heppened on the same button
                if (e.Button.Equals(mPrevClickedButton))
                {
                    RaiseEvent(mMouseDoubleClick, null, e);
                    RaiseEvent(mMouseDoubleClickExtended, null, e);

                    // Stop timer
                    mDoubleClickTimer.Enabled = false;
                    mPrevClickedButton        = MouseButtons.None;
                }
                else
                {
                    // If it was the first click start the timer
                    mDoubleClickTimer.Enabled = true;
                    mPrevClickedButton        = e.Button;
                }
            }
        }
예제 #2
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 MouseHookProcEventHandler(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode >= 0)
            {
                // Marshall the data from callback.
                MouseInput mouseHookStruct = (MouseInput)Marshal.PtrToStructure(lParam, typeof(MouseInput));

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

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

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

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

                case MouseInputNotificationEnum.WM_MBUTTONDOWN:
                    mouseDown  = true;
                    button     = MouseButtons.Middle;
                    clickCount = 1;
                    break;

                case MouseInputNotificationEnum.WM_MBUTTONUP:
                    mouseUp    = true;
                    button     = MouseButtons.Middle;
                    clickCount = 1;
                    break;

                case MouseInputNotificationEnum.WM_MBUTTONDBLCLK:
                    button     = MouseButtons.Middle;
                    clickCount = 2;
                    break;

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

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

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

                case MouseInputNotificationEnum.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);
                    wheel      = true;
                    break;

                case MouseInputNotificationEnum.WM_MOUSEHWHEEL:
                    // If the message is WM_MOUSEHWHEEL, 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
                MouseEventExtendedArgs e = new MouseEventExtendedArgs(
                    button,
                    clickCount,
                    mouseHookStruct.Dx,
                    mouseHookStruct.Dy,
                    mouseDelta);

                // Mouse up
                if (mouseUp)
                {
                    RaiseEvent(MouseUp, null, e);
                    RaiseEvent(MouseUpExtended, null, e);
                }

                // Mouse down
                if (mouseDown)
                {
                    RaiseEvent(MouseDown, null, e);
                    RaiseEvent(MouseDownExtended, null, e);
                }

                // If someone listens to click and a click is happened
                if (clickCount > 0)
                {
                    RaiseEvent(MouseClick, null, e);
                    RaiseEvent(MouseClickExtended, null, e);
                }

                // If someone listens to double click and a click is happened
                if (clickCount == 2)
                {
                    //RaiseEvent(mMouseDoubleClick, e);
                    RaiseEvent(mMouseDoubleClickExtended, null, e);
                }

                // Wheel was moved
                if (mouseDelta != 0 && wheel)
                {
                    RaiseEvent(MouseWheelVertical, null, e);
                    RaiseEvent(MouseWheelVerticalExtended, null, e);
                }
                else if (mouseDelta != 0 && !wheel)
                {
                    RaiseEvent(MouseWheelHorizontal, null, e);
                    RaiseEvent(MouseWheelHorizontalExtended, null, e);
                }

                // If someone listens to move and there was a change in coordinates raise move event
                if ((MouseMove != null || MouseMoveExtended != null) && (mOldX != mouseHookStruct.Dx || mOldY != mouseHookStruct.Dy))
                {
                    mOldX = mouseHookStruct.Dx;
                    mOldY = mouseHookStruct.Dy;

                    RaiseEvent(MouseMove, null, e);
                    RaiseEvent(MouseMoveExtended, null, e);
                }

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

            // call next hook
            return(NativeMethods.CallNextHookEx(new IntPtr(mMouseHookHandle), nCode, wParam, lParam));
        }