Exemple #1
0
        /// <summary>
        /// 鼠标钩子处理函数
        /// </summary>
        /// <param name="nCode"></param>
        /// <param name="wParam"></param>
        /// <param name="lParam"></param>
        /// <returns></returns>
        private int MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode < (int)HookHelper.WH_Codes.HC_ACTION)
            {
                return(Win32Helper.CallNextHookEx(hHook, nCode, wParam, lParam));
            }

            if (OnMouseActivity != null)
            {
                //Marshall the data from callback.
                HookHelper.MouseHookStruct mouseHookStruct = (HookHelper.MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(HookHelper.MouseHookStruct));
                MouseButtons button     = MouseButtons.None;
                short        mouseDelta = 0;
                switch ((int)wParam)
                {
                case (int)HookHelper.WM_MOUSE.WM_LBUTTONDOWN:
                    //case WM_LBUTTONUP:
                    //case WM_LBUTTONDBLCLK:
                    button = MouseButtons.Left;
                    break;

                case (int)HookHelper.WM_MOUSE.WM_RBUTTONDOWN:
                    //case WM_RBUTTONUP:
                    //case WM_RBUTTONDBLCLK:
                    button = MouseButtons.Right;
                    break;

                case (int)HookHelper.WM_MOUSE.WM_MOUSEWHEEL:
                    //button = MouseButtons.Middle;//滚动轮
                    //(value >> 16) & 0xffff; retrieves the high-order word from the given 32-bit value
                    mouseDelta = (short)((mouseHookStruct.MouseData >> 16) & 0xffff);
                    break;
                }

                int clickCount = 0;//点击数
                if (button != MouseButtons.None)
                {
                    if (wParam == (IntPtr)HookHelper.WM_MOUSE.WM_LBUTTONDBLCLK || wParam == (IntPtr)HookHelper.WM_MOUSE.WM_RBUTTONDBLCLK)
                    {
                        clickCount = 2;//双击
                    }
                    else
                    {
                        clickCount = 1;//单击
                    }
                }

                //鼠标事件传递数据
                MouseEventArgs e = new MouseEventArgs(button, clickCount, mouseHookStruct.Point.X, mouseHookStruct.Point.Y, mouseDelta);

                //重写事件
                OnMouseActivity(this, e);
            }

            return(Win32Helper.CallNextHookEx(hHook, nCode, wParam, lParam));
        }
Exemple #2
0
        /// <summary>
        /// 键盘钩子处理函数
        /// </summary>
        /// <param name="code"></param>
        /// <param name="wParam"></param>
        /// <param name="lParam"></param>
        /// <returns></returns>
        private int KeyboardProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            try
            {
                if (nCode < (int)HookHelper.WH_Codes.HC_ACTION)
                {
                    return(Win32Helper.CallNextHookEx((IntPtr)khook, nCode, wParam, lParam));
                }

                //HookHelper.KeyboardHookStruct keyHookStruct = (HookHelper.KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(HookHelper.KeyboardHookStruct));

                #region
                //if ((int)wParam == (int)Keys.C && ((int)lParam & (int)Keys.ControlKey) != 0 ||//Ctrl+C
                //    (int)wParam == (int)Keys.X && ((int)lParam & (int)Keys.ControlKey) != 0)//Ctrl+V
                //{
                //    MessageBox.Show("C||V");
                //}

                //if (lParam.ToInt32() > 0)//捕获键盘按下
                //{
                //    Keys keys = (Keys)wParam;
                //    MessageBox.Show("键盘按下");
                //}
                //if (lParam.ToInt32() < 0)//捕获键盘抬起
                //{
                //    MessageBox.Show("键盘抬起");
                //}

                /****************
                *  //全局键盘钩子判断是否按下键
                *  wParam = = 0x100 // 键盘按下
                *  wParam = = 0x101 // 键盘抬起
                ****************/
                //return 0;//如果返回1,则结束消息,消息截止,不再传递。如果返回0或调用CallNextHookEx函数,消息出这个钩子继续往下传递。
                #endregion
                bool handled = false;
                if (lParam.ToInt32() > 0)                //捕获键盘按下
                {
                    Keys         keyData = (Keys)wParam; //(Keys)keyHookStruct.VKCode;
                    KeyEventArgs e       = new KeyEventArgs(keyData);
                    OnKeyDown.Invoke(this, e);
                    handled = e.Handled;
                }

                //if (this.OnKeyDown != null && (wParam == (IntPtr)HookHelper.WM_KEYBOARD.WM_KEYDOWN || wParam == (IntPtr)HookHelper.WM_KEYBOARD.WM_SYSKEYDOWN))
                //{
                //    Keys keyData = (Keys)keyHookStruct.VKCode;
                //    KeyEventArgs e = new KeyEventArgs(keyData);
                //    OnKeyDown.Invoke(this, e);
                //    handled = e.Handled;
                //}

                if (handled)
                {
                    return(-1);
                }

                return(Win32Helper.CallNextHookEx(khook, nCode, wParam, lParam));
            }
            catch
            {
            }

            return(Win32Helper.CallNextHookEx(khook, nCode, wParam, lParam));
        }