コード例 #1
0
        public static RawInput FromBytes(byte[] bytes, int offset, out int nextByteOffset)
        {
            RawInput ri;

            int headerOffset;
            int dataOffset;

            var header = RawInputHeader.FromBytes(bytes, offset + 0, out headerOffset);

            if ((int)bytes[offset] == (int)RawInputDeviceType.Mouse)
            {
                ri = new RawInput()
                {
                    Header = header,
                    Data   = new RawInputData()
                    {
                        Mouse = RawMouse.FromBytes(bytes, headerOffset, out dataOffset),
                    },
                };
            }
            else if ((int)bytes[offset] == (int)RawInputDeviceType.Keyboard)
            {
                ri = new RawInput()
                {
                    Header = header,
                    Data   = new RawInputData()
                    {
                        Keyboard = RawKeyboard.FromBytes(bytes, headerOffset, out dataOffset),
                    },
                };
            }
            else if ((int)bytes[offset] == (int)RawInputDeviceType.Hid)
            {
                ri = new RawInput()
                {
                    Header = header,
                    Data   = new RawInputData()
                    {
                        Hid = RawHid.FromBytes(bytes, headerOffset, out dataOffset),
                    },
                };
            }
            else
            {
                throw new NotSupportedException();
            }

            nextByteOffset = dataOffset;

            return(ri);
        }
コード例 #2
0
ファイル: Managed.cs プロジェクト: burnsba/BurnsBac.WinApi
        /// <summary>
        /// Calls User32.GetRawInputData. Accepts lParam from WndProc and retrieves the RawInput information.
        /// </summary>
        /// <param name="lParam">Pointer.</param>
        /// <returns>RawInput.</returns>
        public static RawInput GetRawInputData(IntPtr lParam)
        {
            if (lParam == IntPtr.Zero)
            {
                throw new ArgumentException($"{nameof(lParam)} pointer is not set.");
            }

            uint dwSize = 0;
            int  callResult;
            int  win32error;

            IntPtr rawInputBuffer          = IntPtr.Zero;
            bool   rawInputBufferAllocated = false;

            try
            {
                callResult = WinApi.User32.Api.GetRawInputData(lParam, RawInputCommand.Input, IntPtr.Zero, ref dwSize, Marshal.SizeOf(typeof(RawInputHeader)));

                if (callResult != 0)
                {
                    win32error = Marshal.GetLastWin32Error();
                    if (win32error > 0)
                    {
                        throw new Win32ErrorCode($"GetLastWin32Error: {win32error}");
                    }
                    else
                    {
                        throw new BadResultException($"GetRawInputData returned {callResult} (pData == null)");
                    }
                }

                rawInputBuffer          = Marshal.AllocHGlobal((int)dwSize);
                rawInputBufferAllocated = true;
                RawInput ri;

                callResult = WinApi.User32.Api.GetRawInputData(lParam, RawInputCommand.Input, rawInputBuffer, ref dwSize, Marshal.SizeOf(typeof(RawInputHeader)));

                if (callResult != dwSize)
                {
                    win32error = Marshal.GetLastWin32Error();
                    if (win32error > 0)
                    {
                        throw new Win32ErrorCode($"GetLastWin32Error: {win32error}");
                    }
                    else
                    {
                        throw new BadResultException($"GetRawInputData returned {callResult} (retrieving rawInputBuffer, dwSize={dwSize})");
                    }
                }

                var ribytes = new byte[dwSize];
                Marshal.Copy(rawInputBuffer, ribytes, 0, (int)dwSize);
                ri = RawInput.FromBytes(ribytes, 0, out int t);

                return(ri);
            }
            finally
            {
                if (rawInputBufferAllocated)
                {
                    Marshal.FreeHGlobal(rawInputBuffer);
                }
            }
        }