예제 #1
0
        private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            if (msg == User32.WM_INPUT)
            {
                User32.RAWINPUTHEADER header = new User32.RAWINPUTHEADER();
                var headerSize = (uint)Marshal.SizeOf(header);

                uint bufferSize = 0;
                uint res        = User32.GetRawInputData(lParam, User32.RID_INPUT, IntPtr.Zero, ref bufferSize, headerSize);
                if (res == 0)
                {
                    var  dataPtr      = Marshal.AllocHGlobal((int)bufferSize);
                    uint writtenBytes = User32.GetRawInputData(lParam, User32.RID_INPUT, dataPtr, ref bufferSize, headerSize);
                    if (writtenBytes == bufferSize)
                    {
                        var rawInput = Marshal.PtrToStructure <User32.RAWINPUT>(dataPtr);
                        if (rawInput.header.dwType == User32.RIM_TYPEMOUSE)
                        {
                            if ((rawInput.mouse.usButtonFlags & User32.RI_MOUSE_WHEEL) == User32.RI_MOUSE_WHEEL)
                            {
                                Trace.WriteLine($"RawInputListener WM_INPUT MouseWheel: {rawInput.mouse.usButtonData}");
                                MouseWheel?.Invoke(this, rawInput.mouse.usButtonData);
                            }
                        }
                    }
                    Marshal.FreeHGlobal(dataPtr);
                }
            }
            return(IntPtr.Zero);
        }
예제 #2
0
        private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            if (msg == User32.WM_INPUT)
            {
                User32.RAWINPUTHEADER header = new User32.RAWINPUTHEADER();
                var headerSize = (uint)Marshal.SizeOf(header);

                uint bufferSize = 0;
                uint res        = User32.GetRawInputData(lParam, User32.RID_INPUT, IntPtr.Zero, ref bufferSize, headerSize);
                if (res == 0)
                {
                    var  dataPtr      = Marshal.AllocHGlobal((int)bufferSize);
                    uint writtenBytes = User32.GetRawInputData(lParam, User32.RID_INPUT, dataPtr, ref bufferSize, headerSize);
                    if (writtenBytes == bufferSize)
                    {
                        var rawInput = Marshal.PtrToStructure <User32.RAWINPUT>(dataPtr);
                        if (rawInput.header.dwType == User32.RIM_TYPEMOUSE)
                        {
                            if ((rawInput.mouse.usButtonFlags & User32.RI_MOUSE_WHEEL) == User32.RI_MOUSE_WHEEL)
                            {
                                Trace.WriteLine($"RawInputListener WM_INPUT MouseWheel: {rawInput.mouse.usButtonData}");
                                MouseWheel?.Invoke(this, rawInput.mouse.usButtonData);
                            }

                            if ((rawInput.mouse.usFlags & User32.RAWMOUSE_FLAGS.MOUSE_MOVE_ABSOLUTE) == User32.RAWMOUSE_FLAGS.MOUSE_MOVE_ABSOLUTE)
                            {
                                // We don't expect any absolute changes to position.
                                // If we do get one, we assume it's a very large change.
                                Debug.Assert(false);
                                Stop();
                            }
                            else
                            {
                                _currentPosRelative.Y += rawInput.mouse.lLastY;
                                _currentPosRelative.X += rawInput.mouse.lLastX;

                                if (Math.Abs(_currentPosRelative.X) > MAX_MOVE ||
                                    Math.Abs(_currentPosRelative.Y) > MAX_MOVE)
                                {
                                    Stop();
                                }
                            }
                        }
                    }
                    Marshal.FreeHGlobal(dataPtr);
                }
            }
            return(IntPtr.Zero);
        }
예제 #3
0
        public static bool ProcessMouseInputMessage(IntPtr lParam, ref System.Drawing.Point cursorPosition, out int wheelDelta)
        {
            wheelDelta = 0;
            bool isApplicableMouseMessage = false;

            var header     = new User32.RAWINPUTHEADER();
            var headerSize = (uint)Marshal.SizeOf(header);

            uint bufferSize = 0;
            uint res        = User32.GetRawInputData(lParam, User32.RID_INPUT, IntPtr.Zero, ref bufferSize, headerSize);

            if (res == 0)
            {
                var  dataPtr      = Marshal.AllocHGlobal((int)bufferSize);
                uint writtenBytes = User32.GetRawInputData(lParam, User32.RID_INPUT, dataPtr, ref bufferSize, headerSize);
                if (writtenBytes == bufferSize)
                {
                    var rawInput = Marshal.PtrToStructure <User32.RAWINPUT>(dataPtr);
                    if (rawInput.header.dwType == User32.RIM_TYPEMOUSE)
                    {
                        isApplicableMouseMessage = true;

                        if ((rawInput.mouse.usFlags & User32.RAWMOUSE_FLAGS.MOUSE_MOVE_ABSOLUTE) == User32.RAWMOUSE_FLAGS.MOUSE_MOVE_ABSOLUTE)
                        {
                            cursorPosition = System.Windows.Forms.Cursor.Position;
                        }
                        else
                        {
                            cursorPosition.Y += rawInput.mouse.lLastY;
                            cursorPosition.X += rawInput.mouse.lLastX;
                        }

                        if ((rawInput.mouse.usButtonFlags & User32.RI_MOUSE_WHEEL) == User32.RI_MOUSE_WHEEL)
                        {
                            wheelDelta = rawInput.mouse.usButtonData;
                        }
                    }
                }
                Marshal.FreeHGlobal(dataPtr);
            }
            return(isApplicableMouseMessage);
        }