/// <summary>
        /// Retrieving the raw input data struct when a WM_INPUT message has arrived
        /// For Mouse and keyboard we can directly fill the RawInput struct
        /// For HID we need to get RawHeader and RawHid separatly because RawHid is not constant size
        /// </summary>
        /// <param name="LParam">LParam parameter from WindowLoop during WM_INPUT message</param>
        /// <returns></returns>
        private bool GetRawInputData(IntPtr LParam)
        {
            uint HeaderSize = (uint)Marshal.SizeOf(typeof(RawInputHeader));
            uint dwSize     = 0;
            uint size       = (uint)_RawInputHeader.dwSize;

            Win32API.GetRawInputData(LParam, RawInputUiCommand.RID_INPUT, IntPtr.Zero, ref dwSize, (uint)Marshal.SizeOf(typeof(RawInputHeader)));
            uint result;

            if (_dwType == RawInputDeviceType.RIM_TYPEHID)
            {
                IntPtr p = Marshal.AllocHGlobal((int)size);
                result = Win32API.GetRawInputData(LParam, RawInputUiCommand.RID_INPUT, p, ref size, HeaderSize);
                if (result != 0xFFFF && result == size)
                {
                    _RawHidData = RawHid.FromIntPtr((IntPtr)((int)p + HeaderSize));
                    Marshal.FreeHGlobal(p);
                    return(true);
                }
            }
            else if (_dwType == RawInputDeviceType.RIM_TYPEMOUSE)
            {
                result = Win32API.GetRawInputData(LParam, RawInputUiCommand.RID_INPUT, out _RawInputDataMouse, ref size, (uint)Marshal.SizeOf(typeof(RawInputHeader)));
                if (result != 0xFFFF && result == size)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            else if (_dwType == RawInputDeviceType.RIM_TYPEKEYBOARD)
            {
                //TODO
                return(false);
            }

            return(false);
        }
Beispiel #2
0
        /// <summary>
        /// The bRawData array is variable in lenght, and using GetDeviceInfo we can get a pointer to some RawInput structure
        /// containing the RawInputHeader + RawHID structures.
        /// This functions will parse bytes from the pointer to fill a struct with a fixed size byte array as bRawData
        /// </summary>
        /// <param name="Ptr">Pointer to RAWINPUT struct obtained with a call GetRawInputDeviceInfo</param>
        /// <returns>RawHid struct filled with data</returns>
        public static RawHid FromIntPtr(IntPtr Ptr)
        {
            RawHid result = new RawHid();

            //Bytes 0-3 = dwSizeHid
            //Bytes 4-7 = dwCount
            byte[] buffer = new byte[8];
            Marshal.Copy(Ptr, buffer, 0, 8);
            result.dwSizeHid = BitConverter.ToInt32(buffer, 0);
            result.dwCount   = BitConverter.ToInt32(buffer, 4);

            //Creating a fixed size byte array
            result.bRawData = new byte[result.dwCount * result.dwSizeHid];
            try
            {
                Marshal.Copy((IntPtr)((int)Ptr + 8), result.bRawData, 0, result.bRawData.Length);
                return(result);
            }
            catch
            {
                return(result);
            }
        }