コード例 #1
0
        private IntPtr HookFunc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            // Parse system messages
            if (nCode >= 0)
            {
                if (MouseMessages.WM_LBUTTONDOWN == (MouseMessages)wParam)
                {
                    LeftButtonDown?.Invoke(
                        (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT)));
                }
                if (MouseMessages.WM_LBUTTONUP == (MouseMessages)wParam)
                {
                    LeftButtonUp?.Invoke(
                        (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT)));
                }
                if (MouseMessages.WM_RBUTTONDOWN == (MouseMessages)wParam)
                {
                    RightButtonDown?.Invoke(
                        (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT)));
                }
                if (MouseMessages.WM_RBUTTONUP == (MouseMessages)wParam)
                {
                    RightButtonUp?.Invoke(
                        (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT)));
                }
                if (MouseMessages.WM_MOUSEMOVE == (MouseMessages)wParam)
                {
                    MouseMove?.Invoke(
                        (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT)));
                }
                if (MouseMessages.WM_MOUSEWHEEL == (MouseMessages)wParam)
                {
                    MouseWheel?.Invoke(
                        (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT)));
                }
                if (MouseMessages.WM_LBUTTONDBLCLK == (MouseMessages)wParam)
                {
                    DoubleClick?.Invoke(
                        (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT)));
                }
                if (MouseMessages.WM_MBUTTONDOWN == (MouseMessages)wParam)
                {
                    MiddleButtonDown?.Invoke(
                        (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT)));
                }
                if (MouseMessages.WM_MBUTTONUP == (MouseMessages)wParam)
                {
                    MiddleButtonUp?.Invoke(
                        (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT)));
                }
            }

            return(CallNextHookEx(_hookId, nCode, wParam, lParam));
        }
コード例 #2
0
        public override void Update(double dt)
        {
            PrevKeyboard    = CurrentKeyboard;
            CurrentKeyboard = Keyboard.GetState();

            PrevMouse    = CurrentMouse;
            CurrentMouse = Mouse.GetState();

            bool isOverUI = IsOverUI();

            if (CurrentMouse.LeftButton == ButtonState.Pressed)
            {
                if (LeftButtonDown != null && !isOverUI)
                {
                    LeftButtonDown.Invoke(new InputEventArgs(this));
                }
            }

            if (CurrentMouse.RightButton == ButtonState.Pressed)
            {
                if (RightButtonDown != null && !isOverUI)
                {
                    RightButtonDown.Invoke(new InputEventArgs(this));
                }
            }

            if (CurrentMouse.LeftButton == ButtonState.Pressed && PrevMouse.LeftButton == ButtonState.Released)
            {
                if (LeftClick != null && !isOverUI)
                {
                    LeftClick.Invoke(new InputEventArgs(this));
                }
            }

            if (CurrentMouse.RightButton == ButtonState.Pressed && PrevMouse.RightButton == ButtonState.Released)
            {
                if (RightClick != null && !isOverUI)
                {
                    RightClick.Invoke(new InputEventArgs(this));
                }
            }

            foreach (KeyListener listener in _listeners)
            {
                if (CurrentKeyboard.IsKeyDown(listener.Primary) && (listener.Continuous || !PrevKeyboard.IsKeyDown(listener.Primary)))
                {
                    listener.Trigger(listener.Primary, this);
                }
                else if (CurrentKeyboard.IsKeyDown(listener.Alternate) && (listener.Continuous || !PrevKeyboard.IsKeyDown(listener.Alternate)))
                {
                    listener.Trigger(listener.Alternate, this);
                }
            }
        }
コード例 #3
0
ファイル: Mouse.cs プロジェクト: thomasvt/glyphedit
        public void Update()
        {
            var mouseState = _mouse.GetState();

            if (mouseState.LeftButton != _previousMouseState.LeftButton)
            {
                if (mouseState.LeftButton == ButtonState.Pressed)
                {
                    LeftButtonDown?.Invoke(this, new MouseEventArgs(ref mouseState));
                }
                else
                {
                    LeftButtonUp?.Invoke(this, new MouseEventArgs(ref mouseState));
                }
            }

            if (mouseState.MiddleButton != _previousMouseState.MiddleButton)
            {
                if (mouseState.MiddleButton == ButtonState.Pressed)
                {
                    MiddleButtonDown?.Invoke(this, new MouseEventArgs(ref mouseState));
                }
                else
                {
                    MiddleButtonUp?.Invoke(this, new MouseEventArgs(ref mouseState));
                }
            }

            if (mouseState.RightButton != _previousMouseState.RightButton)
            {
                if (mouseState.RightButton == ButtonState.Pressed)
                {
                    RightButtonDown?.Invoke(this, new MouseEventArgs(ref mouseState));
                }
                else
                {
                    RightButtonUp?.Invoke(this, new MouseEventArgs(ref mouseState));
                }
            }

            if (mouseState.Position != _previousMouseState.Position)
            {
                MouseMove?.Invoke(this, new MouseMoveEventArgs(mouseState, _previousMouseState.Position));
            }

            if (mouseState.ScrollWheelValue != _previousMouseState.ScrollWheelValue)
            {
                MouseWheel?.Invoke(this, new MouseWheelEventArgs(mouseState, mouseState.ScrollWheelValue - _previousMouseState.ScrollWheelValue));
            }

            _previousMouseState = mouseState;
        }
コード例 #4
0
        public static void Update()
        {
            MouseState mState = Mouse.GetState();

            if (mState.LeftButton == ButtonState.Pressed)
            {
                if (mouseReleased == true)
                {
                    // Left button down!
                    mouseReleased     = false;
                    downClickPosition = new Vector2(mState.Position.X, mState.Position.Y);
                    LeftButtonDown?.Invoke(
                        null,
                        new MouseClickEventArgs(new Vector2(mState.Position.X, mState.Position.Y))
                        );
                }
                // Swipe on the screen.
                else
                {
                    // Left button down and swipe!
                    if (downClickPosition.X > 0)
                    {
                        var distance = Vector2.Distance(downClickPosition, new Vector2(mState.Position.X, mState.Position.Y));
                        if (distance > distanceForDetectedSwipe)
                        {
                            Direction direction = GetSwipeDirection(downClickPosition, new Vector2(mState.Position.X, mState.Position.Y));
                            LeftButtonSwipe?.Invoke(
                                null,
                                new MouseSwipeEventArgs(
                                    downClickPosition,
                                    new Vector2(mState.Position.X, mState.Position.Y),
                                    direction
                                    )
                            {
                            }
                                );
                        }
                    }
                }
            }
            else if (mState.LeftButton == ButtonState.Released && mouseReleased == false)
            {
                // Left button up!
                mouseReleased     = true;
                downClickPosition = new Vector2(-1, -1);
                LeftButtonUp?.Invoke(
                    null,
                    new MouseClickEventArgs(new Vector2(mState.Position.X, mState.Position.Y))
                    );
            }
        }
コード例 #5
0
        /// <summary>
        ///     Callback function
        /// </summary>
        private IntPtr HookFunc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            // parse system messages
            if (nCode < 0)
            {
                return(NativeMethods.CallNextHookEx(HookId, nCode, wParam, lParam));
            }
            // ReSharper disable once SwitchStatementMissingSomeCases
            switch ((MouseFlags)wParam)
            {
            case MouseFlags.LeftDown:
                LeftButtonDown?.Invoke((MouseInput)Marshal.PtrToStructure(lParam, typeof(MouseInput)));
                break;

            case MouseFlags.LeftUp:
                LeftButtonUp?.Invoke(MouseInputToStructure(lParam));
                break;

            case MouseFlags.RightDown:
                RightButtonDown?.Invoke(MouseInputToStructure(lParam));
                break;

            case MouseFlags.RightUp:
                RightButtonUp?.Invoke(MouseInputToStructure(lParam));
                break;

            case MouseFlags.Move:
                MouseMove?.Invoke(MouseInputToStructure(lParam));
                break;

            case MouseFlags.Wheel:
                MouseWheel?.Invoke(MouseInputToStructure(lParam));
                break;

            case MouseFlags.DoubleLeftClick:
                DoubleClick?.Invoke(MouseInputToStructure(lParam));
                break;

            case MouseFlags.MiddleDown:
                MiddleButtonDown?.Invoke(MouseInputToStructure(lParam));
                break;

            case MouseFlags.MiddleUp:
                MiddleButtonUp?.Invoke(MouseInputToStructure(lParam));
                break;
            }

            return(NativeMethods.CallNextHookEx(HookId, nCode, wParam, lParam));
        }
コード例 #6
0
        /// <summary>
        /// Callback function
        /// </summary>
        protected override IntPtr HookFunc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            // parse system messages
            if (nCode >= 0)
            {
                switch ((WM_MESSAGE)wParam)
                {
                case WM_MESSAGE.WM_LBUTTONDOWN:
                    LeftButtonDown?.Invoke((MOUSEINPUT)Marshal.PtrToStructure(lParam, typeof(MOUSEINPUT)));
                    break;

                case WM_MESSAGE.WM_LBUTTONUP:
                    LeftButtonUp?.Invoke((MOUSEINPUT)Marshal.PtrToStructure(lParam, typeof(MOUSEINPUT)));
                    break;

                case WM_MESSAGE.WM_RBUTTONDOWN:
                    RightButtonDown?.Invoke((MOUSEINPUT)Marshal.PtrToStructure(lParam, typeof(MOUSEINPUT)));
                    break;

                case WM_MESSAGE.WM_RBUTTONUP:
                    RightButtonUp?.Invoke((MOUSEINPUT)Marshal.PtrToStructure(lParam, typeof(MOUSEINPUT)));
                    break;

                case WM_MESSAGE.WM_MOUSEMOVE:
                    MouseMove?.Invoke((MOUSEINPUT)Marshal.PtrToStructure(lParam, typeof(MOUSEINPUT)));
                    break;

                case WM_MESSAGE.WM_MOUSEWHEEL:
                    MouseWheel?.Invoke((MOUSEINPUT)Marshal.PtrToStructure(lParam, typeof(MOUSEINPUT)));
                    break;

                case WM_MESSAGE.WM_LBUTTONDBLCLK:
                    DoubleClick?.Invoke((MOUSEINPUT)Marshal.PtrToStructure(lParam, typeof(MOUSEINPUT)));
                    break;

                case WM_MESSAGE.WM_MBUTTONDOWN:
                    MiddleButtonDown?.Invoke((MOUSEINPUT)Marshal.PtrToStructure(lParam, typeof(MOUSEINPUT)));
                    break;

                case WM_MESSAGE.WM_MBUTTONUP:
                    MiddleButtonUp?.Invoke((MOUSEINPUT)Marshal.PtrToStructure(lParam, typeof(MOUSEINPUT)));
                    break;
                }
            }
            return(CallNextHookEx(hookID, nCode, wParam, lParam));
        }
コード例 #7
0
ファイル: MouseHook.cs プロジェクト: PeteyPii/Winook
        protected override void OnMessageReceived(object sender, MessageEventArgs e)
        {
            Contract.Requires(e != null);

            var eventArgs = new MouseMessageEventArgs
            {
                MessageCode = BitConverter.ToInt32(e.Bytes, 0),
                X           = BitConverter.ToInt32(e.Bytes, 4),
                Y           = BitConverter.ToInt32(e.Bytes, 8),
                Handle      = BitConverter.ToInt32(e.Bytes, 12),
                HitTestCode = BitConverter.ToInt32(e.Bytes, 16),
                Delta       = BitConverter.ToInt16(e.Bytes, 20),
            };

            Debug.WriteLine($"Code: {eventArgs.MessageCode}; X: {eventArgs.X}; Y: {eventArgs.Y}; Delta: {eventArgs.Delta}");

            MessageReceived?.Invoke(this, eventArgs);

            switch (GetMessageCode(eventArgs.MessageCode))
            {
            case MouseMessageCode.MouseMove:
                MouseMove?.Invoke(this, eventArgs);
                break;

            case MouseMessageCode.LeftButtonDown:
                LeftButtonDown?.Invoke(this, eventArgs);
                break;

            case MouseMessageCode.LeftButtonUp:
                LeftButtonUp?.Invoke(this, eventArgs);
                break;

            case MouseMessageCode.LeftButtonDblClk:
                LeftButtonDblClk?.Invoke(this, eventArgs);
                break;

            case MouseMessageCode.RightButtonDown:
                RightButtonDown?.Invoke(this, eventArgs);
                break;

            case MouseMessageCode.RightButtonUp:
                RightButtonUp?.Invoke(this, eventArgs);
                break;

            case MouseMessageCode.RightButtonDblClk:
                RightButtonDblClk?.Invoke(this, eventArgs);
                break;

            case MouseMessageCode.MiddleButtonDown:
                MiddleButtonDown?.Invoke(this, eventArgs);
                break;

            case MouseMessageCode.MiddleButtonUp:
                MiddleButtonUp?.Invoke(this, eventArgs);
                break;

            case MouseMessageCode.MiddleButtonDblClk:
                MiddleButtonDblClk?.Invoke(this, eventArgs);
                break;

            case MouseMessageCode.MouseWheel:
                MouseWheel?.Invoke(this, eventArgs);
                break;

            case MouseMessageCode.XButtonDown:
                XButtonDown?.Invoke(this, eventArgs);
                break;

            case MouseMessageCode.XButtonUp:
                XButtonUp?.Invoke(this, eventArgs);
                break;

            case MouseMessageCode.XButtonDblClk:
                XButtonDblClk?.Invoke(this, eventArgs);
                break;

            case MouseMessageCode.MouseHWheel:
                MouseHWheel?.Invoke(this, eventArgs);
                break;

            default:
                break;
            }

            if (_messageHandlers.ContainsKey(eventArgs.MessageCode))
            {
                _messageHandlers[eventArgs.MessageCode]?.Invoke(this, eventArgs);
            }
        }
コード例 #8
0
        /// <summary>
        /// Callback function
        /// </summary>
        private IntPtr HookFunc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            // parse system messages
            if (nCode >= 0)
            {
                var data    = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));
                var message = (MouseMessages)wParam;

                var isDown = false;
                var isUp   = false;
                SimpleMouseButtons button;


                switch (message)
                {
                case MouseMessages.WM_MOUSEMOVE:
                    break;

                case MouseMessages.WM_LBUTTONDOWN:
                    isDown = true;
                    break;

                case MouseMessages.WM_RBUTTONDOWN:
                    isDown = true;
                    break;

                case MouseMessages.WM_MBUTTONDOWN:
                    isDown = true;
                    break;

                case MouseMessages.WM_LBUTTONUP:
                    isUp = true;
                    break;

                case MouseMessages.WM_RBUTTONUP:
                    isUp = true;
                    break;

                case MouseMessages.WM_MBUTTONUP:
                    isUp = true;
                    break;

                case MouseMessages.WM_MOUSEWHEEL:
                    break;

                case MouseMessages.WM_MOUSEHWHEEL:
                    break;

                case MouseMessages.WM_LBUTTONDBLCLK:
                    break;

                case MouseMessages.WM_RBUTTONDBLCLK:
                    break;

                case MouseMessages.WM_MBUTTONDBLCLK:
                    break;

                case MouseMessages.WM_XBUTTONDOWN:
                    isDown = true;
                    break;

                case MouseMessages.WM_XBUTTONUP:
                    isUp = true;
                    break;

                case MouseMessages.WM_XBUTTONDBLCLK:
                    break;

                default:
                    break;
                }


                if (MouseMessages.WM_LBUTTONDOWN == message)
                {
                    LeftButtonDown?.Invoke(data);
                }
                if (MouseMessages.WM_LBUTTONUP == message)
                {
                    LeftButtonUp?.Invoke(data);
                }
                if (MouseMessages.WM_RBUTTONDOWN == message)
                {
                    RightButtonDown?.Invoke(data);
                }
                if (MouseMessages.WM_RBUTTONUP == message)
                {
                    RightButtonUp?.Invoke(data);
                }
                if (MouseMessages.WM_MOUSEMOVE == message)
                {
                    MouseMove?.Invoke(data);
                }
                if (MouseMessages.WM_MOUSEWHEEL == message)
                {
                    MouseWheel?.Invoke(data);
                }
                if (MouseMessages.WM_LBUTTONDBLCLK == message)
                {
                    DoubleClick?.Invoke(data);
                }
                if (MouseMessages.WM_MBUTTONDOWN == message)
                {
                    MiddleButtonDown?.Invoke(data);
                }
                if (MouseMessages.WM_MBUTTONUP == message)
                {
                    MiddleButtonUp?.Invoke(data);
                }

                //Generic Event, fire with type
                MouseEvent?.Invoke(data, message);
            }
            return(CallNextHookEx(hookID, nCode, wParam, lParam));
        }
コード例 #9
0
 private void OnLeftButtonDown(MouseHookEventArgs e)
 {
     LeftButtonDown?.Invoke(this, e);
     OnMouseEvent(e);
 }
コード例 #10
0
ファイル: MouseHook.cs プロジェクト: macote/Winook
        protected override void OnMessageReceived(object sender, MessageEventArgs e)
        {
            Contract.Requires(e != null);

            var modifiers = BitConverter.ToUInt16(e.Bytes, 24);
            var eventArgs = new MouseMessageEventArgs
            {
                MessageCode = BitConverter.ToInt32(e.Bytes, 0),
                X           = BitConverter.ToInt32(e.Bytes, 4),
                Y           = BitConverter.ToInt32(e.Bytes, 8),
                Handle      = BitConverter.ToInt32(e.Bytes, 12),
                HitTestCode = BitConverter.ToInt32(e.Bytes, 16),
                Modifiers   = modifiers,
                Shift       = (modifiers & 0b100) > 0,
                Control     = (modifiers & 0b10) > 0,
                Alt         = (modifiers & 0b1) > 0,
            };

            var messageCode = GetMessageCode(eventArgs.MessageCode);

            if (messageCode == MouseMessageCode.MouseWheel)
            {
                eventArgs.Delta = BitConverter.ToInt16(e.Bytes, 20);
            }
            else if (messageCode == MouseMessageCode.NCXButtonDown ||
                     messageCode == MouseMessageCode.NCXButtonUp ||
                     messageCode == MouseMessageCode.NCXButtonDblClk ||
                     messageCode == MouseMessageCode.XButtonDown ||
                     messageCode == MouseMessageCode.XButtonUp ||
                     messageCode == MouseMessageCode.XButtonDblClk)
            {
                eventArgs.XButtons = BitConverter.ToInt16(e.Bytes, 20);
            }

            Debug.Write($"Code: {eventArgs.MessageCode}; X: {eventArgs.X}; Y: {eventArgs.Y}; Modifiers: {eventArgs.Modifiers:x}; ");
            Debug.WriteLine($"Delta: {eventArgs.Delta}; XButtons: {eventArgs.XButtons}");

            MessageReceived?.Invoke(this, eventArgs);

            switch (messageCode)
            {
            case MouseMessageCode.MouseMove:
                MouseMove?.Invoke(this, eventArgs);
                break;

            case MouseMessageCode.LeftButtonDown:
                LeftButtonDown?.Invoke(this, eventArgs);
                break;

            case MouseMessageCode.LeftButtonUp:
                LeftButtonUp?.Invoke(this, eventArgs);
                break;

            case MouseMessageCode.LeftButtonDblClk:
                LeftButtonDblClk?.Invoke(this, eventArgs);
                break;

            case MouseMessageCode.RightButtonDown:
                RightButtonDown?.Invoke(this, eventArgs);
                break;

            case MouseMessageCode.RightButtonUp:
                RightButtonUp?.Invoke(this, eventArgs);
                break;

            case MouseMessageCode.RightButtonDblClk:
                RightButtonDblClk?.Invoke(this, eventArgs);
                break;

            case MouseMessageCode.MiddleButtonDown:
                MiddleButtonDown?.Invoke(this, eventArgs);
                break;

            case MouseMessageCode.MiddleButtonUp:
                MiddleButtonUp?.Invoke(this, eventArgs);
                break;

            case MouseMessageCode.MiddleButtonDblClk:
                MiddleButtonDblClk?.Invoke(this, eventArgs);
                break;

            case MouseMessageCode.MouseWheel:
                MouseWheel?.Invoke(this, eventArgs);
                break;

            case MouseMessageCode.XButtonDown:
                XButtonDown?.Invoke(this, eventArgs);
                break;

            case MouseMessageCode.XButtonUp:
                XButtonUp?.Invoke(this, eventArgs);
                break;

            case MouseMessageCode.XButtonDblClk:
                XButtonDblClk?.Invoke(this, eventArgs);
                break;

            case MouseMessageCode.MouseHWheel:
                MouseHWheel?.Invoke(this, eventArgs);
                break;

            default:
                break;
            }

            if (_messageHandlers.ContainsKey(eventArgs.MessageCode))
            {
                _messageHandlers[eventArgs.MessageCode]?.Invoke(this, eventArgs);
            }
        }