/// <summary>
        /// 判断是否执行鼠标的单击操作
        /// </summary>
        /// <param wParam="IntPtr">鼠标键值</param>
        /// <param lParam="IntPtr">指针</param>
        private IntPtr MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            EventHandler <MouseExEventArgs> handler = this.Events[EventMouseActivity] as EventHandler <MouseExEventArgs>;

            if ((nCode >= 0) && (handler != null))
            {
                MouseLLHookStruct mouseHookStruct = (MouseLLHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseLLHookStruct)); //定义鼠示托管对象
                MouseButtons      button          = MouseButtons.None;                                                            //实例化MouseButtons类,用于记录鼠标曾按下的数量
                short             mouseDelta      = 0;
                switch ((int)wParam)                                                                                              //鼠标的键值
                {
                case WM_LBUTTONDOWN:                                                                                              //鼠标左键
                    button = MouseButtons.Left;                                                                                   //记录鼠标左键的单击次数
                    break;

                case WM_RBUTTONDOWN:             //鼠标右键
                    button = MouseButtons.Right; //记录鼠标右键的单击次数
                    break;

                case WM_MOUSEWHEEL:                                                               //鼠标中键
                    mouseDelta = unchecked ((short)((mouseHookStruct.mouseData >> 16) & 0xffff)); //获取鼠标中键的键值
                    break;
                }
                //实例化MouseExEventArgs类
                MouseExEventArgs e = new MouseExEventArgs(button, 0, mouseHookStruct.pt.x, mouseHookStruct.pt.y, mouseDelta, mouseHookStruct.flags);
                handler(this, e);                                      //处理事件
            }
            return(CallNextHookEx(hMouseHook, nCode, wParam, lParam)); //调用下一个勾子过程
        }
        /// <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 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
                short             mouseDelta = (short)((mouseHookStruct.MouseData >> 16) & 0xffff);
                MouseEventExtArgs e          = new MouseEventExtArgs(0, 0, 0, 0, mouseDelta);

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

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

            //call next hook
            return(CallNextHookEx(s_MouseHookHandle, nCode, wParam, lParam));
        }
        private static int MouseHookProc(int nCode, int wParam, IntPtr lParam)
        {
            if (nCode >= 0)
            {
                MouseLLHookStruct mouseHookStruct = (MouseLLHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseLLHookStruct));

                Point mPos = new Point(mouseHookStruct.Point.X, mouseHookStruct.Point.Y);

                switch (wParam)
                {
                case WM_LBUTTONDOWN:
                    m_isDown = true;
                    SuperMouseClick(mPos, true);
                    break;

                case WM_LBUTTONUP:
                    m_isDown = false;
                    SuperMouseClick(mPos, false);
                    break;
                }

                if ((SuperMouseMove != null) && (m_OldX != mouseHookStruct.Point.X || m_OldY != mouseHookStruct.Point.Y))
                {
                    m_OldX = mouseHookStruct.Point.X;
                    m_OldY = mouseHookStruct.Point.Y;

                    SuperMouseMove(mPos, m_isDown);
                }
            }

            return(0);
        }
        private int MouseHookProc(int nCode, int wParam, IntPtr lParam)
        {
            if (nCode >= 0 && this.OnMouseActivity != null)
            {
                MouseLLHookStruct mouseLLHookStruct = (MouseLLHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseLLHookStruct));
                MouseButtons      mouseButtons      = MouseButtons.None;
                short             delta             = 0;
                switch (wParam)
                {
                case 513:
                    mouseButtons = MouseButtons.Left;
                    break;

                case 516:
                    mouseButtons = MouseButtons.Right;
                    break;

                case 522:
                    delta = (short)((mouseLLHookStruct.mouseData >> 16) & 0xFFFF);
                    break;
                }
                int clicks = 0;
                if (mouseButtons != 0)
                {
                    clicks = ((wParam != 515 && wParam != 518) ? 1 : 2);
                }
                MouseEventArgs e = new MouseEventArgs(mouseButtons, clicks, mouseLLHookStruct.pt.x, mouseLLHookStruct.pt.y, delta);
                this.OnMouseActivity(this, e);
            }
            return(CallNextHookEx(hMouseHook, nCode, wParam, lParam));
        }
Example #5
0
        protected override int HookCallbackProcedure(int nCode, int wParam, IntPtr lParam)
        {
            if (nCode > -1 && (MouseLog != null))
            {
                MouseLLHookStruct mouseHookStruct =
                    (MouseLLHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseLLHookStruct));
                int            clickCount = 1;
                MouseButton    button     = GetButton(wParam, out clickCount);
                MouseEventType eventType  = GetEventType(wParam);
                var            deltaData  = (eventType == MouseEventType.MouseWheel
                    ? (short)((mouseHookStruct.mouseData >> 16) & 0xffff)
                    : 0);

                // Prevent multiple Right Click events (this probably happens for popup menus)
                if (button == MouseButton.Right && mouseHookStruct.flags != 0)
                {
                    eventType = MouseEventType.None;
                }
                var mouseEventArgs = new HookMouseEventArgs
                {
                    ClickButton     = button,
                    MouseEventType  = eventType,
                    ClickCount      = clickCount,
                    MousePosition   = new Point(mouseHookStruct.pt.x, mouseHookStruct.pt.y),
                    MouseWheelDelta = deltaData,
                    EventTimeStamp  = DateTime.Now
                };
                if (MonitorMouseMove || eventType != MouseEventType.MouseMove)
                {
                    MouseLog(mouseEventArgs);
                }
            }

            return(CallNextHookEx(_handleToHook, nCode, wParam, lParam));
        }
Example #6
0
        /// <summary>A callback function which will be called every Time a mouse activity detected.</summary>
        /// <param name="nCode">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">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">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 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;

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

                //If someone listens to move and there was a change in coordinates raise move event
                if ((s_MouseMove != 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 (e.Handled)
                {
                    return(-1);
                }
            }

            //call next hook
            return(CallNextHookEx(s_MouseHookHandle, nCode, wParam, lParam));
        }
Example #7
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 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));

                bool passInput = true;

                if (s_MouseDown != null)
                {
                    if (wParam == WM_LBUTTONDOWN)
                    {
                        passInput = s_MouseDown(MouseButtons.Left, mouseHookStruct);
                    }
                    else if (wParam == WM_RBUTTONDOWN)
                    {
                        passInput = s_MouseDown(MouseButtons.Right, mouseHookStruct);
                    }
                    else if (wParam == WM_MBUTTONDOWN)
                    {
                        passInput = s_MouseDown(MouseButtons.Middle, mouseHookStruct);
                    }
                    else if (wParam == WM_XBUTTONDOWN)
                    {
                        passInput = s_MouseDown(mouseHookStruct.IsXButton1 ? MouseButtons.XButton1 : MouseButtons.XButton2,
                                                mouseHookStruct);
                    }
                }
                if (s_MouseUp != null)
                {
                    if (wParam == WM_LBUTTONUP)
                    {
                        passInput = s_MouseUp(MouseButtons.Left, mouseHookStruct);
                    }
                    else if (wParam == WM_RBUTTONUP)
                    {
                        passInput = s_MouseUp(MouseButtons.Right, mouseHookStruct);
                    }
                    else if (wParam == WM_MBUTTONUP)
                    {
                        passInput = s_MouseUp(MouseButtons.Middle, mouseHookStruct);
                    }
                    else if (wParam == WM_XBUTTONUP)
                    {
                        passInput = s_MouseUp(mouseHookStruct.IsXButton1 ? MouseButtons.XButton1 : MouseButtons.XButton2,
                                              mouseHookStruct);
                    }
                }


                if (!passInput)
                {
                    return(1);
                }
            }

            return(CallNextHookEx(s_MouseHookHandle, nCode, wParam, lParam));
        }
Example #8
0
        private int MouseHookProc(int nCode, int wParam, IntPtr lParam)
        {
            // if ok and someone listens to our events
            //bool Disable = false;
            if ((nCode >= 0) && (OnMouseActivity != null))
            {
                //Marshall the data from callback.
                MouseLLHookStruct mouseHookStruct = (MouseLLHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseLLHookStruct));

                //detect button clicked
                MouseButtons button     = MouseButtons.None;
                short        mouseDelta = 0;
                switch (wParam)
                {
                case WM_LBUTTONDOWN:
                    //case WM_LBUTTONUP:
                    //case WM_LBUTTONDBLCLK:
                    button = MouseButtons.Left;
                    break;

                case WM_RBUTTONDOWN:
                    //case WM_RBUTTONUP:
                    //case WM_RBUTTONDBLCLK:
                    button = MouseButtons.Right;
                    //Disable = true;
                    break;

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

                //double clicks
                int clickCount = 0;
                if (button != MouseButtons.None)
                {
                    if (wParam == WM_LBUTTONDBLCLK || wParam == WM_RBUTTONDBLCLK)
                    {
                        clickCount = 2;
                    }
                    else
                    {
                        clickCount = 1;
                    }
                }

                //generate event
                MouseEventArgs e = new MouseEventArgs(
                    button,
                    clickCount,
                    mouseHookStruct.pt.x,
                    mouseHookStruct.pt.y,
                    mouseDelta);
                //raise it
                OnMouseActivity(this, e);
            }
            //call next hook
            return(CallNextHookEx(hMouseHook, nCode, wParam, lParam));
        }
Example #9
0
        private static Stroke GetStroke(uint msg, ref MouseLLHookStruct s)
        {
            switch (msg)
            {
            case 0x0200:                     // WM_MOUSEMOVE
                return(Stroke.MOVE);

            case 0x0201:                     // WM_LBUTTONDOWN
                return(Stroke.LEFT_DOWN);

            case 0x0202:                     // WM_LBUTTONUP
                return(Stroke.LEFT_UP);

            case 0x0204:                     // WM_RBUTTONDOWN
                return(Stroke.RIGHT_DOWN);

            case 0x0205:                     // WM_RBUTTONUP
                return(Stroke.RIGHT_UP);

            case 0x0207:                     // WM_MBUTTONDOWN
                return(Stroke.MIDDLE_DOWN);

            case 0x0208:                     // WM_MBUTTONUP
                return(Stroke.MIDDLE_UP);

            case 0x020A:                     // WM_MOUSEWHEEL
                return(((short)((s.mouseData >> 16) & 0xffff) > 0) ? Stroke.WHEEL_UP : Stroke.WHEEL_DOWN);

            case 0x020B:                     // WM_XBUTTONDOWN
                switch (s.mouseData >> 16)
                {
                case 1:
                    return(Stroke.X1_DOWN);

                case 2:
                    return(Stroke.X2_DOWN);

                default:
                    return(Stroke.UNKNOWN);
                }

            case 0x020C:                     // WM_XBUTTONUP
                switch (s.mouseData >> 16)
                {
                case 1:
                    return(Stroke.X1_UP);

                case 2:
                    return(Stroke.X2_UP);

                default:
                    return(Stroke.UNKNOWN);
                }

            default:
                return(Stroke.UNKNOWN);
            }
        }
 private void Action(MouseLLHookStruct data, MouseMessages message)
 {
     var strCaption = string.Format("time = {3}, x = {0}, y = {1}, flags = {4}, message = {2}\r\n",
         data.pt.x.ToString("d"),
         data.pt.y.ToString("d"),
         (int)message,
         data.time,
         data.flags);
     var bytes = Encoding.ASCII.GetBytes(strCaption);
     fs.Write(bytes, 0, bytes.Length);
 }
        private void Action(MouseLLHookStruct data, MouseMessages message)
        {
            var strCaption = string.Format("time = {3}, x = {0}, y = {1}, flags = {4}, message = {2}\r\n",
                                           data.pt.x.ToString("d"),
                                           data.pt.y.ToString("d"),
                                           (int)message,
                                           data.time,
                                           data.flags);
            var bytes = Encoding.ASCII.GetBytes(strCaption);

            fs.Write(bytes, 0, bytes.Length);
        }
Example #12
0
        protected override int HookCallbackProcedure(int nCode, int wParam, IntPtr lParam)
        {
            if (nCode > -1 && (MouseDown != null || MouseUp != null || MouseMove != null))
            {
                MouseLLHookStruct mouseHookStruct =
                    (MouseLLHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseLLHookStruct));

                MouseButtons   button    = GetButton(wParam);
                MouseEventType eventType = GetEventType(wParam);

                MouseEventArgs e = new MouseEventArgs(
                    button,
                    (eventType == MouseEventType.DoubleClick ? 2 : 1),
                    mouseHookStruct.pt.x,
                    mouseHookStruct.pt.y,
                    (eventType == MouseEventType.MouseWheel ? (short)((mouseHookStruct.mouseData >> 16) & 0xffff) : 0));

                // Prevent multiple Right Click events (this probably happens for popup menus)
                if (button == MouseButtons.Right && mouseHookStruct.flags != 0)
                {
                    eventType = MouseEventType.None;
                }

                switch (eventType)
                {
                case MouseEventType.MouseDown:
                    MouseDown?.Invoke(this, e);
                    break;

                case MouseEventType.MouseUp:
                    Click?.Invoke(this, new EventArgs());
                    MouseUp?.Invoke(this, e);
                    break;

                case MouseEventType.DoubleClick:
                    DoubleClick?.Invoke(this, new EventArgs());
                    break;

                case MouseEventType.MouseWheel:
                    MouseWheel?.Invoke(this, e);
                    break;

                case MouseEventType.MouseMove:
                    MouseMove?.Invoke(this, e);
                    break;

                default:
                    break;
                }
            }
            return(CallNextHookEx(_handleToHook, nCode, wParam, lParam));
        }
        //-------------------------------------------------------------------------------

        private int MouseHookProc(int nCode, int wParam, IntPtr lParam)
        {
            if ((nCode >= 0) && (OnMouseActivity != null))
            {
                MouseLLHookStruct mouseHookStruct = (MouseLLHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseLLHookStruct));

                MouseButtons button     = MouseButtons.None;
                short        mouseDelta = 0;
                int          clickCount = 0;
                switch (wParam)
                {
                case WM_LBUTTONDOWN:        //513出现了
                    button     = MouseButtons.Left;
                    clickCount = 1;
                    break;

                case WM_RBUTTONDOWN:        //516出现了
                    button     = MouseButtons.Right;
                    clickCount = 1;
                    break;

                case WM_LBUTTONDBLCLK:        //515  doubleclick没有出现过
                    button     = MouseButtons.XButton1;
                    clickCount = 2;
                    break;

                case WM_RBUTTONDBLCLK:        //518
                    button     = MouseButtons.XButton1;
                    clickCount = 2;
                    break;

                case WM_MOUSEMOVE:        //512 出现了
                    button     = MouseButtons.XButton2;
                    clickCount = 0;
                    break;

                case WM_MOUSEWHEEL:        //522 没试
                    mouseDelta = (short)((mouseHookStruct.mouseData >> 16) & 0xffff);
                    clickCount = 0;
                    break;
                }

                MouseEventArgs e = new MouseEventArgs(
                    button,
                    clickCount,
                    mouseHookStruct.pt.x,
                    mouseHookStruct.pt.y,
                    mouseDelta);
                OnMouseActivity(this, e);    //转给委托函数
            }
            return(CallNextHookEx(hMouseHook, nCode, wParam, lParam));
        }
Example #14
0
        private int MouseHookProc(int nCode, int wParam, IntPtr lParam)
        {
            if ((nCode >= 0) && (OnMouseActivity != null))
            {
                MouseLLHookStruct mouseHookStruct = (MouseLLHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseLLHookStruct));

                MouseButtons button     = MouseButtons.None;
                short        mouseDelta = 0;
                isClose = false;
                switch (wParam)
                {
                case WM_LBUTTONDOWN:
                    button = MouseButtons.Left;
                    break;

                case WM_RBUTTONDOWN:
                    button = MouseButtons.Right;
                    break;

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

                case WM_XBUTTONDOWN:
                    Stop();
                    break;
                }

                int clickCount = 0;
                if (button != MouseButtons.None)
                {
                    if (wParam == WM_LBUTTONDBLCLK || wParam == WM_RBUTTONDBLCLK)
                    {
                        clickCount = 2;
                    }
                    else
                    {
                        clickCount = 1;
                    }
                }

                MouseEventArgs e = new MouseEventArgs(
                    button,
                    clickCount,
                    mouseHookStruct.pt.x,
                    mouseHookStruct.pt.y,
                    mouseDelta);
                OnMouseActivity(this, e);
            }
            return(CallNextHookEx(hMouseHook, nCode, wParam, lParam));
        }
Example #15
0
        /// <summary>
        /// A callback function which will be called every time a mouse activity detected.
        /// </summary>
        /// <param name="nCode"></param>
        /// <param name="wParam"></param>
        /// <param name="lParam"></param>
        /// <returns></returns>
        private int MouseHookProc(int nCode, int wParam, IntPtr lParam)
        {
            // Something listens to events.
            if (nCode >= 0 && OnMouseActivity != null)
            {
                // Marshall the data from callback.
                MouseLLHookStruct mouseLLHookStruct = (MouseLLHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseLLHookStruct));

                // Detect button clicked.
                MouseButtons button     = 0;
                short        mouseDelta = 0;
                switch (wParam)
                {
                case WM_LBUTTONDOWN:
                    button = MouseButtons.Left;
                    break;

                case WM_RBUTTONDOWN:
                    button = MouseButtons.Right;
                    break;

                case WM_MOUSEWHEEL:
                    mouseDelta = (short)((mouseLLHookStruct.mouseData >> 16) & 0xfff);
                    break;
                }

                int clickCount = 0;
                if (button != 0)
                {
                    if (wParam == WM_LBUTTONDBLCLK || wParam == WM_RBUTTONDBLCLK)
                    {
                        clickCount = 2;
                    }
                    else
                    {
                        clickCount = 1;
                    }
                }

                System.Windows.Forms.MouseEventArgs e = new System.Windows.Forms.MouseEventArgs(button, clickCount, mouseLLHookStruct.p.x, mouseLLHookStruct.p.y, mouseDelta);
                OnMouseActivity(this, e);
            }
            return(CallNextHookEx(hMouseHook, nCode, wParam, lParam));
        }
Example #16
0
        private int MouseHookProc(int code, int wparam, IntPtr lparam)
        {
            MouseHookEventArgs args = (MouseHookEventArgs)null;

            if (code >= 0 && this.mouseActivity != null)
            {
                MouseLLHookStruct mouseLlHookStruct = (MouseLLHookStruct)Marshal.PtrToStructure(lparam, typeof(MouseLLHookStruct));
                MouseButtons      buttons           = MouseButtons.None;
                short             num = (short)0;
                switch (wparam)
                {
                case NativeMethods.WM_LBUTTONDOWN:
                    buttons = MouseButtons.Left;
                    break;

                case NativeMethods.WM_RBUTTONDOWN:
                    buttons = MouseButtons.Right;
                    break;

                case NativeMethods.WM_MOUSEWHEEL:
                    num = (short)(mouseLlHookStruct.mouseData >> 16 & (int)ushort.MaxValue);
                    break;
                }
                int clicks = 0;
                if (buttons != MouseButtons.None)
                {
                    clicks = wparam == NativeMethods.WM_LBUTTONDBLCLK || wparam == NativeMethods.WM_RBUTTONDBLCLK ? 2 : 1;
                }
                args = new MouseHookEventArgs(buttons, clicks, mouseLlHookStruct.pt.X, mouseLlHookStruct.pt.Y, (int)num);
                this.mouseActivity((object)this, args);
            }
            if (args == null || !args.Handled)
            {
                return(NativeMethods.CallNextHookEx(this.mouseHookHandle, code, wparam, lparam));
            }
            else
            {
                return(1);
            }
        }
Example #17
0
        private static int MouseHookProcLL(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode >= 0)
            {
                // Marshall the data from the callback.
                MouseLLHookStruct hookStruct = (MouseLLHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseLLHookStruct));

                if (GlobalMouseMoveHandler != null)
                {
                    var e = new MouseMoveEventArgs(new Point(hookStruct.Point.x, hookStruct.Point.y));

                    if (_globalLastPosition.X != e.Position.X || _globalLastPosition.Y != e.Position.Y)
                    {
                        GlobalMouseMoveHandler.Invoke(null, e);

                        _globalLastPosition = new Point(e.Position.X, e.Position.Y);
                    }
                }
            }

            return(CallNextHookEx(hHookLL, nCode, wParam, lParam));
        }
Example #18
0
        private static IntPtr MouseHookProc(int nCode, uint msg, ref MouseLLHookStruct s)
        {
            if (nCode >= 0)
            {
                state.stroke    = GetStroke(msg, ref s);
                state.X         = s.pt.x;
                state.Y         = s.pt.y;
                state.Data      = s.mouseData;
                state.Flags     = s.flags;
                state.Time      = s.time;
                state.ExtraInfo = s.dwExtraInfo;

                hookHandler?.Invoke(state);

                if (IsCancel)
                {
                    IsCancel = false;
                    return((IntPtr)1);
                }
            }

            return(CallNextHookEx(hHook, nCode, msg, ref s));
        }
Example #19
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 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_MBUTTONDOWN:
                    mouseDown  = true;
                    button     = MouseButtons.Middle;
                    clickCount = 1;
                    break;

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

                //滑鼠側鍵
                case WM_XBUTTONDOWN:
                    mouseDown  = true;
                    button     = MouseButtons.XButton1;
                    clickCount = 1;
                    break;

                case WM_XBUTTONUP:
                    mouseUp    = true;
                    button     = MouseButtons.XButton1;
                    clickCount = 1;
                    break;

                /*case WM_NCXBUTTONDOWN:
                 *  mouseDown = true;
                 *  button = MouseButtons.XButton2;
                 *  clickCount = 1;
                 *  break;
                 * case WM_NCXBUTTONUP:
                 *  mouseUp = true;
                 *  button = MouseButtons.XButton2;
                 *  clickCount = 1;
                 *  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
                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) {
                 *      //System.Console.WriteLine("mmm");
                 *      s_MouseMove.Invoke(null, e);
                 *  }
                 *
                 *  if (s_MouseMoveExt != null) {
                 *      s_MouseMoveExt.Invoke(null, e);
                 *  }
                 *
                 * } */

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


                /*if (button != MouseButtons.None) {
                 *  System.Console.WriteLine($"wParam:{wParam} type:{((mouseDown)?"down":"up")}  button:{button}  bool_m_handled:{Mouse2Touch.Form1.bool_m_handled}");
                 * }*/

                if (Mouse2Touch.Form1.bool_stop == false)  //強制終止所有操作


                //事件攔截
                {
                    if (Mouse2Touch.Form1.mtype == Mouse2Touch.Form1.Mtype.滑鼠側鍵 && button == MouseButtons.XButton1)
                    {
                        return(-1);
                    }
                    else if (Mouse2Touch.Form1.mtype == Mouse2Touch.Form1.Mtype.滑鼠右鍵 && button == MouseButtons.Right && Mouse2Touch.Form1.bool_m_handled)
                    {
                        return(-1);
                    }
                    else if (Mouse2Touch.Form1.mtype == Mouse2Touch.Form1.Mtype.滑鼠右鍵_2 && button == MouseButtons.Right)
                    {
                        return(-1);

                        /*} else if (Mouse2Touch.Form1.mtype == Mouse2Touch.Form1.Mtype.滑鼠左鍵 && button == MouseButtons.Left && Mouse2Touch.Form1.bool_m_handled) {
                         *  return -1;*/
                    }
                    else if (Mouse2Touch.Form1.mtype == Mouse2Touch.Form1.Mtype.滑鼠中鍵 && button == MouseButtons.Middle)
                    {
                        return(-1);
                    }
                }
            }

            //call next hook
            return(CallNextHookEx(s_MouseHookHandle, nCode, wParam, lParam));
        }
Example #20
0
        protected override int HookCallbackProcedure(int nCode, int wParam, IntPtr lParam)
        {
            if (nCode > -1 && (MouseDown != null || MouseUp != null || MouseMove != null))
            {
                MouseLLHookStruct mouseHookStruct =
                    (MouseLLHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseLLHookStruct));

                MouseButtons   button    = GetButton(wParam);
                MouseEventType eventType = GetEventType(wParam);

                if (eventType == MouseEventType.MouseMove)
                {
                    return(CallNextHookEx(_handleToHook, nCode, wParam, lParam));
                }

                MouseEventArgs e = new MouseEventArgs(
                    button,
                    (eventType == MouseEventType.DoubleClick ? 2 : 1),
                    mouseHookStruct.pt.x,
                    mouseHookStruct.pt.y,
                    (eventType == MouseEventType.MouseWheel ? (short)((mouseHookStruct.mouseData >> 16) & 0xffff) : 0));

                // Prevent multiple Right Click events (this probably happens for popup menus)
                if (button == MouseButtons.Right && mouseHookStruct.flags != 0)
                {
                    eventType = MouseEventType.None;
                }

                foreach (var item in notInputKeys)
                {
                    if (item.key == Keys.LButton && button == MouseButtons.Left)
                    {
                        return(1);
                    }
                    else if (item.key == Keys.RButton && button == MouseButtons.Right)
                    {
                        return(1);
                    }
                    else if (item.key == Keys.MButton && button == MouseButtons.Middle)
                    {
                        return(1);
                    }
                    else if (item.key == Keys.XButton1 && button == MouseButtons.XButton1)
                    {
                        return(1);
                    }
                    else if (item.key == Keys.XButton2 && button == MouseButtons.XButton2)
                    {
                        return(1);
                    }
                }

                switch (eventType)
                {
                case MouseEventType.MouseDown:
                    if (MouseDown != null)
                    {
                        MouseDown(this, e);
                    }
                    break;

                case MouseEventType.MouseUp:
                    if (Click != null)
                    {
                        Click(this, new EventArgs());
                    }
                    if (MouseUp != null)
                    {
                        MouseUp(this, e);
                    }
                    break;

                case MouseEventType.DoubleClick:
                    if (DoubleClick != null)
                    {
                        DoubleClick(this, new EventArgs());
                    }
                    break;

                case MouseEventType.MouseWheel:
                    if (MouseWheel != null)
                    {
                        MouseWheel(this, e);
                    }
                    break;

                case MouseEventType.MouseMove:
                    if (MouseMove != null)
                    {
                        MouseMove(this, e);
                    }
                    break;

                default:
                    break;
                }
            }

            return(CallNextHookEx(_handleToHook, nCode, wParam, lParam));
        }
        /// <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 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:
                    //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
                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));
        }
Example #22
0
 protected override void WndProc(ref Message m)
 {
     //if (Enum.IsDefined(typeof(MESSAGE), m.Msg))
     //{
     //    Debug.Print(((MESSAGE)m.Msg).ToString());
     //}
     if (m.Msg == Win32.User.WM_SYSCOMMAND && (int)m.WParam == Win32.User.SC_CLOSE)
     {
         this.Hide();
     }
     else
     {
         if (m.Msg == WM_HOTKEY)
         {
             int id = m.WParam.ToInt32();
             DoCommand(CommandPairs[id]);
         }
         else if (m.Msg == Win32.User.WM_CREATE)
         {
             myHook.SetHook(this.Handle, MyHook.HookType.OnlyMouse);
         }
         else if (m.Msg == Win32.User.WM_DESTROY || m.Msg == Win32.User.WM_QUIT || m.Msg == Win32.User.WM_CLOSE)
         {
             netClient1.DisConnect();
             myHook.SetUnhook();
             notifyIcon1.Visible = false;
             Application.Exit();
         }
         else if (m.Msg == WM_MYMSG)
         {
             if ((int)m.WParam == WM_MOUSEWHEEL)
             {
                 MouseLLHookStruct mouse = (MouseLLHookStruct)Marshal.PtrToStructure(m.LParam, typeof(MouseLLHookStruct));
                 short             delta = (short)(mouse.mouseData >> 16);
                 if (delta > 0)
                 {
                     foreach (var key in UpCommand)
                     {
                         if (key.Keys.KeyModifiers == Control.ModifierKeys)
                         {
                             DoCommand(key);
                         }
                     }
                 }
                 else
                 {
                     foreach (var key in DownCommand)
                     {
                         if (key.Keys.KeyModifiers == Control.ModifierKeys)
                         {
                             DoCommand(key);
                         }
                     }
                 }
             }
             else if ((int)m.WParam == Win32.User.WM_LBUTTONDOWN)
             {
                 foreach (var key in LButtonCommand)
                 {
                     if (key.Keys.KeyModifiers == Control.ModifierKeys)
                     {
                         DoCommand(key);
                     }
                 }
             }
             else if ((int)m.WParam == Win32.User.WM_RBUTTONDOWN)
             {
                 foreach (var key in RButtonCommand)
                 {
                     if (key.Keys.KeyModifiers == Control.ModifierKeys)
                     {
                         DoCommand(key);
                     }
                 }
             }
             else if ((int)m.WParam == Win32.User.WM_MBUTTONDOWN)
             {
                 foreach (var key in MButtonCommand)
                 {
                     if (key.Keys.KeyModifiers == Control.ModifierKeys)
                     {
                         DoCommand(key);
                     }
                 }
             }
         }
         base.WndProc(ref m);
     }
 }
Example #23
0
        protected override IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode >= 0 && wParam == (IntPtr)MouseWheel)
            {
                hookStruct = (MouseLLHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseLLHookStruct));
                if (hookStruct.mouseData > 0)
                {
                    wheelValue = -1;
                }
                else
                {
                    wheelValue = 1;
                }
            }

            return CallNextHookEx(HookID, nCode, wParam, lParam);
        }
Example #24
0
        protected override int HookCallbackProcedure(int nCode, int wParam, IntPtr lParam)
        {
            if ((nCode > -1) && ((MouseDown != null) || (MouseUp != null) || (MouseMove != null)))
            {
                MouseLLHookStruct _mouseHookStruct =
                    (MouseLLHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseLLHookStruct));

                MouseButtons   _button    = GetButton(wParam);
                MouseEventType _eventType = GetEventType(wParam);

                MouseEventArgs _e = new MouseEventArgs(
                    _button,
                    (_eventType == MouseEventType.DoubleClick ? 2 : 1),
                    _mouseHookStruct.pt.x,
                    _mouseHookStruct.pt.y,
                    (_eventType == MouseEventType.MouseWheel ? (short)((_mouseHookStruct.mouseData >> 16) & 0xffff) : 0));

                // Prevent multiple Right Click events (this probably happens for popup menus)
                if ((_button == MouseButtons.Right) && (_mouseHookStruct.flags != 0))
                {
                    _eventType = MouseEventType.None;
                }

                switch (_eventType)
                {
                case MouseEventType.MouseDown:
                    if (MouseDown != null)
                    {
                        MouseDown(this, _e);
                    }

                    break;

                case MouseEventType.MouseUp:
                    if (Click != null)
                    {
                        Click(this, new EventArgs());
                    }

                    if (MouseUp != null)
                    {
                        MouseUp(this, _e);
                    }

                    break;

                case MouseEventType.DoubleClick:
                    if (DoubleClick != null)
                    {
                        DoubleClick(this, new EventArgs());
                    }

                    break;

                case MouseEventType.MouseWheel:
                    if (MouseWheel != null)
                    {
                        MouseWheel(this, _e);
                    }

                    break;

                case MouseEventType.MouseMove:
                    if (MouseMove != null)
                    {
                        MouseMove(this, _e);
                    }

                    break;

                default:
                    break;
                }
            }

            return(CallNextHookEx(m_handleToHook, nCode, wParam, lParam));
        }
Example #25
0
        private void mouseHook_Callback(int nCode, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            //Marshall the data from callback.
            MouseLLHookStruct mouseHookStruct =
                (MouseLLHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseLLHookStruct));

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

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

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

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

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

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

            case WM_MBUTTONUP:
                mouseUp = true;
                button  = MouseButtons.Middle;
                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);
                break;

            case WM_XBUTTONDOWN:
                mouseDown = true;
                switch (mouseHookStruct.MouseData >> 16)
                {
                case XBUTTON1:
                    button = MouseButtons.XButton1;
                    break;

                case XBUTTON2:
                    button = MouseButtons.XButton2;
                    break;
                }
                break;

            case WM_XBUTTONUP:
                mouseUp = true;
                switch (mouseHookStruct.MouseData >> 16)
                {
                case XBUTTON1:
                    button = MouseButtons.XButton1;
                    break;

                case XBUTTON2:
                    button = MouseButtons.XButton2;
                    break;
                }
                break;
            }

            //generate event
            MouseEventExtArgs e = new MouseEventExtArgs(
                button,
                mouseUp || mouseDown ? 1 : 0,
                mouseHookStruct.Point.X,
                mouseHookStruct.Point.Y,
                mouseDelta);

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

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

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

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

            handled = handled || e.Handled;

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

                MouseMove.Invoke(null, e);
                handled = handled || (e.Handled && (int)wParam == WM_MOUSEMOVE);
            }
        }
Example #26
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 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));

				MouseButtons buttonClicked = MouseButtons.None;
				bool mouseDown = false;
				bool mouseUp = false;

				switch (wParam)
				{
					case WM_LBUTTONDOWN:
						mouseDown = true;
						buttonClicked = MouseButtons.Left;
						break;
					case WM_LBUTTONUP:
						mouseUp = true;
						buttonClicked = MouseButtons.Left;
						break;
					case WM_RBUTTONDOWN:
						mouseDown = true;
						buttonClicked = MouseButtons.Right;
						break;
					case WM_RBUTTONUP:
						mouseUp = true;
						buttonClicked = MouseButtons.Right;
						break;
				}

				// generate event 
				MouseEventExtArgs e = new MouseEventExtArgs(buttonClicked, 1, mouseHookStruct.Point.X, mouseHookStruct.Point.Y, 0);

				#region Invoke Subscribed Events
				if (_mouseUpEvent != null && mouseUp)
					_mouseUpEvent.Invoke(null, e);

				if (_mouseDownEvent != null && mouseDown)
					_mouseDownEvent.Invoke(null, e);

				if (_mouseClickEvent != null && buttonClicked != MouseButtons.None)
					_mouseClickEvent.Invoke(null, e);

				if (_mouseMoveEvent != null && (_oldX != mouseHookStruct.Point.X || _oldY != mouseHookStruct.Point.Y))
				{
					_oldX = mouseHookStruct.Point.X;
					_oldY = mouseHookStruct.Point.Y;

					if (_mouseMoveEvent != null)
						_mouseMoveEvent.Invoke(null, e);
				}
				#endregion

				// do not call the next hook if we've handle this event
				if (e.Handled)
					return -1;
			}

			// call next hook
			return CallNextHookEx(_mouseHookHandle, nCode, wParam, lParam);
		}
Example #27
0
        /// <summary>
        /// 鼠标检测活动将被称为每次回调函数
        /// </summary>
        private static int MouseHookProc(int nCode, int wParam, IntPtr lParam)
        {
            if (nCode >= 0)
            {
                //Marshall 从回调的数据
                MouseLLHookStruct mouseHookStruct = (MouseLLHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseLLHookStruct));

                //检测按钮点击
                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:
                    //如果消息是WM_MOUSEWHEEL,MouseData成员是滚轮。
                    //一个轮击定义为WHEEL_DELTA,这是120。
                    //(value >> 16) & 0xffff; 从给定的32位值检索高位字。
                    mouseDelta = (short)((mouseHookStruct.MouseData >> 16) & 0xffff);

                    //TODO: X BUTTONS (这个按钮暂时没有测试)
                    break;
                }

                //生成事件
                MouseEventExtArgs e = new MouseEventExtArgs(
                    button,
                    clickCount,
                    mouseHookStruct.Point.X,
                    mouseHookStruct.Point.Y,
                    mouseDelta);

                //鼠标弹起
                if (s_MouseUp != null && mouseUp)
                {
                    s_MouseUp.Invoke(null, e);
                }

                //鼠标按下
                if (s_MouseDown != null && mouseDown)
                {
                    s_MouseDown.Invoke(null, e);
                }

                //单击并点击时触发
                if (s_MouseClick != null && clickCount > 0)
                {
                    s_MouseClick.Invoke(null, e);
                }

                //单击并点击时触发
                if (s_MouseClickExt != null && clickCount > 0)
                {
                    s_MouseClickExt.Invoke(null, e);
                }

                //单击或双击时触发
                if (s_MouseDoubleClick != null && clickCount == 2)
                {
                    s_MouseDoubleClick.Invoke(null, e);
                }

                //鼠标滚轮滚动
                if (s_MouseWheel != null && mouseDelta != 0)
                {
                    s_MouseWheel.Invoke(null, e);
                }

                //触发滚轮滚动
                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);
                }
            }

            //调用下一个钩子
            return(CallNextHookEx(s_MouseHookHandle, nCode, wParam, lParam));
        }
            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));
            }
Example #29
0
        private static int MouseHookProc(int nCode, int wParam, IntPtr lParam)
        {
            if (nCode >= 0)
            {
                MouseLLHookStruct mouseHookStruct = (MouseLLHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseLLHookStruct));

                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;
                }

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

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

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

                if (s_MouseClick != null && clickCount > 0)
                {
                    s_MouseClick.Invoke(null, e);
                }

                if (s_MouseClickExt != null && clickCount > 0)
                {
                    s_MouseClickExt.Invoke(null, e);
                }

                if (s_MouseDoubleClick != null && clickCount == 2)
                {
                    s_MouseDoubleClick.Invoke(null, e);
                }

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

                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);
                }
            }

            return(CallNextHookEx(s_MouseHookHandle, nCode, wParam, lParam));
        }
Example #30
0
        private void HookEx_Events(object sender, WindowsHookExEventArgs e)
        {
            if (MouseChange == null)
            {
                return;
            }
            MouseLLHookStruct @struct   = (MouseLLHookStruct)Marshal.PtrToStructure((IntPtr)e.lParam, typeof(MouseLLHookStruct));
            PressType         pressType = PressType.None;
            VirtualKeys       vk        = VirtualKeys.None;
            int click = 0;

            switch (e.wParam.ToInt64())
            {
            // left
            case WM.LBUTTONDOWN:
                pressType = PressType.KeyDown;
                vk        = VirtualKeys.LeftButton;
                click     = 1;
                break;

            case WM.LBUTTONUP:
                pressType = PressType.KeyUp;
                vk        = VirtualKeys.LeftButton;
                click     = 1;
                break;

            case WM.LBUTTONDBLCLK:
                pressType = PressType.LeftButtonDoubleClick;
                break;

            // right
            case WM.RBUTTONDOWN:
                pressType = PressType.KeyDown;
                vk        = VirtualKeys.RightButton;
                click     = 1;
                break;

            case WM.RBUTTONUP:
                pressType = PressType.KeyUp;
                vk        = VirtualKeys.RightButton;
                click     = 1;
                break;

            case WM.RBUTTONDBLCLK:
                pressType = PressType.RightButtonDoubleClick;
                break;

            // middle
            case WM.MBUTTONDOWN:
                pressType = PressType.KeyDown;
                vk        = VirtualKeys.MiddleButton;
                click     = 1;
                break;

            case WM.MBUTTONUP:
                pressType = PressType.KeyUp;
                vk        = VirtualKeys.MiddleButton;
                click     = 1;
                break;

            case WM.MBUTTONDBLCLK:
                pressType = PressType.MiddleButtonDoubleClick;
                break;

            // wheel
            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
                var mouseDelta = (short)((@struct.MouseData >> 16) & 0xffff);
                if (mouseDelta > 0)
                {
                    pressType = PressType.WheelUp;
                }
                else
                {
                    pressType = PressType.WheelDown;
                }
                break;
                //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.
            }
            var arg = new MouseChangeEventArgs()
            {
                Handled       = false,
                PressType     = pressType,
                Key           = vk,
                MouseClick    = click,
                MousePosition = @struct.Point,
            };

            if (LastMousePosX != @struct.Point.X || LastMousePosY != @struct.Point.Y)
            {
                LastMousePosX  = @struct.Point.X;
                LastMousePosY  = @struct.Point.Y;
                arg.MouseMoved = true;
            }
            foreach (var dele in MouseChange.GetInvocationList().Reverse())
            {
                dele.DynamicInvoke(this, arg);
                if (arg.Handled)
                {
                    e.Handled = true;
                    return;
                }
            }
        }
Example #31
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 ok and someone listens to our events
            if ((nCode >= 0) && (OnMouseActivity != null))
            {
                //Marshall the data from callback.
                MouseLLHookStruct mouseHookStruct = (MouseLLHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseLLHookStruct));

                //detect button clicked
                MouseButtons button     = MouseButtons.None;
                short        mouseDelta = 0;
                switch (wParam)
                {
                case WM_LBUTTONDOWN:
                    //case WM_LBUTTONUP:
                    //case WM_LBUTTONDBLCLK:
                    button = MouseButtons.Left;
                    break;

                case WM_RBUTTONDOWN:
                    //case WM_RBUTTONUP:
                    //case WM_RBUTTONDBLCLK:
                    button = MouseButtons.Right;
                    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;
                }

                //double clicks
                int clickCount = 0;
                if (button != MouseButtons.None)
                {
                    if (wParam == WM_LBUTTONDBLCLK || wParam == WM_RBUTTONDBLCLK)
                    {
                        clickCount = 2;
                    }
                    else
                    {
                        clickCount = 1;
                    }
                }

                //generate event
                MouseEventArgs e = new MouseEventArgs(
                    button,
                    clickCount,
                    mouseHookStruct.pt.x,
                    mouseHookStruct.pt.y,
                    mouseDelta);
                //raise it
                OnMouseActivity(this, e);
            }
            //call next hook
            return(CallNextHookEx(hMouseHook, nCode, wParam, lParam));
        }
Example #32
0
        protected override int HookCallbackProcedure(int nCode, int wParam, IntPtr lParam)
        {
            if (Enabled && nCode > -1)
            {
                MouseLLHookStruct mouseHookStruct =
                    (MouseLLHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseLLHookStruct));

                //Console.WriteLine("X:{0}\tY:{1}\tFlag:{2}", mouseHookStruct.pt.x, mouseHookStruct.pt.y, mouseHookStruct.flags);
                if (mouseHookStruct.flags == 0x00000000) //evento reale -> scarta
                {
                    return(1);
                }

                MouseButtons   button    = GetButton(wParam);
                MouseEventType eventType = GetEventType(wParam);

                MouseEventArgs e = new MouseEventArgs(
                    button,
                    (eventType == MouseEventType.DoubleClick ? 2 : 1),
                    mouseHookStruct.pt.x,
                    mouseHookStruct.pt.y,

                    (eventType == MouseEventType.MouseWheel ? (short)((mouseHookStruct.mouseData >> 16) & 0xffff) : 0));

                // Prevent multiple Right Click events (this probably happens for popup menus)
                if (button == MouseButtons.Right && mouseHookStruct.flags != 0)
                {
                    eventType = MouseEventType.None;
                }



                switch (eventType)
                {
                case MouseEventType.MouseDown:
                    if (MouseDown != null)
                    {
                        MouseDown(this, e);
                    }
                    //return 1;
                    break;

                case MouseEventType.MouseUp:
                    if (Click != null)
                    {
                        Click(this, new EventArgs());
                    }
                    if (MouseUp != null)
                    {
                        MouseUp(this, e);
                    }
                    break;

                case MouseEventType.DoubleClick:
                    if (DoubleClick != null)
                    {
                        DoubleClick(this, new EventArgs());
                    }
                    break;

                case MouseEventType.MouseWheel:
                    if (MouseWheel != null)
                    {
                        MouseWheel(this, e);
                    }
                    break;

                case MouseEventType.MouseMove:
                    if (MouseMove != null)
                    {
                        MouseMove(this, e);
                    }
                    //return 1;
                    break;

                default:
                    break;
                }
            }

            return(CallNextHookEx(_handleToHook, nCode, wParam, lParam));
            //return 1;
        }