Esempio n. 1
0
        /// <summary>
        /// Procedure for mouse hook
        /// </summary>
        /// <param name="nCode"></param>
        /// <param name="wParam"></param>
        /// <param name="lParam"></param>
        /// <returns></returns>
        protected override int HookProc(int nCode, Int32 wParam, IntPtr lParam)
        {
            if (nCode >= 0)
            {
                //Try to disable mouse actions.
                if (DisableMove)
                {
                    return(1);
                }
                else if (DisableWheel & wParam == (int)WM_MOUSE.WM_MOUSEWHEEL)
                {
                    return(1);
                }
                else if (DisableButton)
                {
                    if (wParam != (int)WM_MOUSE.WM_MOUSEFIRST && wParam != (int)WM_MOUSE.WM_MOUSEMOVE && wParam != (int)WM_MOUSE.WM_MOUSEWHEEL)
                    {
                        return(1);
                    }
                }
            }


            if ((nCode >= 0) && (OnMouseActivity != null))
            {
                //Marshall the data from callback.
                MouseHookStruct hookStruct = (MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseHookStruct));

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

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

                case (int)WM_MOUSE.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)((hookStruct.MouseData >> 16) & 0xffff);
                    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.
                }

                //double clicks
                int clickCount = 0;
                if (button != MouseButtons.None)
                {
                    if (log.IsDebugEnabled)
                    {
                        log.Debug(String.Format("Captured mouse button. code {0}, wparam {1}, lparam {2}.", nCode, wParam, lParam));
                    }

                    if (wParam == (int)WM_MOUSE.WM_LBUTTONDBLCLK || wParam == (int)WM_MOUSE.WM_RBUTTONDBLCLK)
                    {
                        clickCount = 2;
                    }
                    else
                    {
                        clickCount = 1;
                    }
                }

                //generate event
                MouseEventArgs e = new MouseEventArgs(button, clickCount, hookStruct.Point.X, hookStruct.Point.Y, mouseDelta);
                //raise it
                OnMouseActivity(this, e);
            }

            return(Win32Helper.CallNextHookEx(this._hook, nCode, wParam, lParam));
        }
Esempio n. 2
0
        /// <summary>
        /// 键盘钩子处理函数
        /// </summary>
        /// <param name="nCode"></param>
        /// <param name="wParam"></param>
        /// <param name="lParam"></param>
        /// <returns></returns>
        /// <remarks>此版本的键盘事件处理不是很好,还有待修正.</remarks>
        protected override int HookProc(int nCode, Int32 wParam, IntPtr lParam)
        {
            if (log.IsDebugEnabled)
            {
                log.Debug(String.Format("Captured key press. code {0}, wparam {1}, lparam {2}.", nCode, wParam, lParam));
            }

            //Disable keyboard.
            if (DisableDevice)
            {
                bool notExceptKey = true;
                if (nCode >= 0)
                {
                    KeyboardHookStruct keyStruct = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));
                    Keys keyData = (Keys)keyStruct.VKCode;
                    foreach (Keys key in ExceptKeys)
                    {
                        if (key.Equals(keyData))
                        {
                            if (log.IsDebugEnabled)
                            {
                                log.Debug(String.Format("Captured hot key '{0}' when disable keyboard press.", keyData));
                            }
                            notExceptKey = false;
                            break;
                        }
                    }
                }

                if (notExceptKey)
                {
                    return(1);
                }
            }

            bool handled = false;

            //it was ok and someone listens to events
            if (nCode >= 0)
            {
                KeyboardHookStruct keyStruct = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));

                //raise KeyDown
                if (OnKeyDown != null && (wParam == (int)WM_KEYBOARD.WM_KEYDOWN || wParam == (int)WM_KEYBOARD.WM_SYSKEYDOWN))
                {
                    Keys         keyData = (Keys)keyStruct.VKCode;
                    KeyEventArgs e       = new KeyEventArgs(keyData);
                    this.OnKeyDown(this, e);
                    handled = handled || e.Handled;
                }

                // raise KeyUp
                if (this.OnKeyUp != null && (wParam == (int)WM_KEYBOARD.WM_KEYUP || wParam == (int)WM_KEYBOARD.WM_SYSKEYUP))
                {
                    Keys         keyData = (Keys)keyStruct.VKCode;
                    KeyEventArgs e       = new KeyEventArgs(keyData);
                    this.OnKeyUp(this, e);
                    handled = handled || e.Handled;
                }


                // raise KeyPress
                if (this.OnKeyPress != null && wParam == (int)WM_KEYBOARD.WM_KEYUP)
                {
                    bool isDownShift = false, isDownCapslock = false;
                    try {
                        isDownShift    = ((Win32Helper.GetKeyStates(VK_SHIFT) & 0x80) == 0x80 ? true : false);
                        isDownCapslock = (Win32Helper.GetKeyStates(VK_CAPITAL) != 0 ? true : false);
                    } catch {
                        //
                    }

                    byte[] keyState = new byte[256];
                    Win32Helper.GetKeyboardState(keyState);
                    byte[] inBuffer = new byte[2];
                    int    result   = Win32Helper.ToAscii(keyStruct.VKCode, keyStruct.ScanCode, keyState, inBuffer, keyStruct.Flags);
                    if (log.IsDebugEnabled)
                    {
                        log.Debug(String.Format("Key converting result is '{0}'", result));
                    }

                    if (result == 1)
                    {
                        char key = (char)inBuffer[0];

                        if ((isDownCapslock ^ isDownShift) && Char.IsLetter(key))
                        {
                            key = Char.ToUpper(key);
                        }

                        if (log.IsDebugEnabled)
                        {
                            log.Debug(String.Format("Captured key press '{0}'", key));
                        }

                        KeyPressEventArgs e = new KeyPressEventArgs(key);
                        this.OnKeyPress(this, e);

                        handled = handled || e.Handled;
                    }
                }
            }

            //if event handled in application do not handoff to other listeners
            if (handled)
            {
                return(1);
            }
            else
            {
                return(Win32Helper.CallNextHookEx(this._hook, nCode, wParam, lParam));
            }
        }