示例#1
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 HookType.WM_LBUTTONDOWN:
                    mouseDown  = true;
                    button     = MouseButtons.Left;
                    clickCount = 1;
                    break;

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

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

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

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

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

                case HookType.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(HookApi.CallNextHookEx(s_MouseHookHandle, nCode, wParam, lParam));
        }
        /// <summary> 键盘检测活动将被称为每次回调函数。 </summary>
        private static int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)
        {
            //表示如有underlaing事件设置e.Handled标志
            bool handled = false;

            if (nCode >= 0)
            {
                //在lParam中读取KeyboardHookStruct结构
                KeyboardHookStruct MyKeyboardHookStruct = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));
                if (s_KeyDown != null && (wParam == HookType.WM_KEYDOWN || wParam == HookType.WM_SYSKEYDOWN))
                {
                    Keys         keyData = (Keys)MyKeyboardHookStruct.VirtualKeyCode;
                    KeyEventArgs e       = new KeyEventArgs(keyData);
                    s_KeyDown.Invoke(null, e);
                    handled = e.Handled;
                }

                // 按键按下
                if (s_KeyPress != null && wParam == HookType.WM_KEYDOWN)
                {
                    bool isDownShift    = ((HookApi.GetKeyState(HookType.VK_SHIFT) & 0x80) == 0x80 ? true : false);
                    bool isDownCapslock = (HookApi.GetKeyState(HookType.VK_CAPITAL) != 0 ? true : false);

                    byte[] keyState = new byte[256];
                    HookApi.GetKeyboardState(keyState);
                    byte[] inBuffer = new byte[2];
                    if (HookApi.ToAscii(MyKeyboardHookStruct.VirtualKeyCode,
                                        MyKeyboardHookStruct.ScanCode,
                                        keyState,
                                        inBuffer,
                                        MyKeyboardHookStruct.Flags) == 1)
                    {
                        char key = (char)inBuffer[0];
                        if ((isDownCapslock ^ isDownShift) && Char.IsLetter(key))
                        {
                            key = Char.ToUpper(key);
                        }
                        KeyPressEventArgs e = new KeyPressEventArgs(key);
                        s_KeyPress.Invoke(null, e);
                        handled = handled || e.Handled;
                    }
                }

                // 按键弹起
                if (s_KeyUp != null && (wParam == HookType.WM_KEYUP || wParam == HookType.WM_SYSKEYUP))
                {
                    Keys         keyData = (Keys)MyKeyboardHookStruct.VirtualKeyCode;
                    KeyEventArgs e       = new KeyEventArgs(keyData);
                    s_KeyUp.Invoke(null, e);
                    handled = handled || e.Handled;
                }
            }

            //如果事件在应用程序处理的不换手到其他听众
            if (handled)
            {
                return(-1);
            }

            //转发到其它应用程序
            return(HookApi.CallNextHookEx(s_KeyboardHookHandle, nCode, wParam, lParam));
        }