RawInput.cs
changeset 10 17f8421146ba
child 17 8f7e35c3bfd1
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/RawInput.cs	Fri Nov 07 22:12:22 2014 +0100
     1.3 @@ -0,0 +1,94 @@
     1.4 +using System;
     1.5 +using System.Runtime.InteropServices;
     1.6 +using System.Diagnostics;
     1.7 +
     1.8 +
     1.9 +namespace Win32
    1.10 +{
    1.11 +    static class RawInput
    1.12 +    {
    1.13 +        /// <summary>
    1.14 +        /// 
    1.15 +        /// </summary>
    1.16 +        /// <param name="aRawInputHandle"></param>
    1.17 +        /// <param name="aRawInput"></param>
    1.18 +        /// <param name="rawInputBuffer">Caller must free up memory on the pointer using Marshal.FreeHGlobal</param>
    1.19 +        /// <returns></returns>
    1.20 +        public static bool GetRawInputData(IntPtr aRawInputHandle, ref RAWINPUT aRawInput, ref IntPtr rawInputBuffer)
    1.21 +        {
    1.22 +            bool success = true;
    1.23 +            rawInputBuffer = IntPtr.Zero;
    1.24 +
    1.25 +            try
    1.26 +            {
    1.27 +                uint dwSize = 0;
    1.28 +                uint sizeOfHeader = (uint)Marshal.SizeOf(typeof(RAWINPUTHEADER));
    1.29 +
    1.30 +                //Get the size of our raw input data.
    1.31 +                Win32.Function.GetRawInputData(aRawInputHandle, Const.RID_INPUT, IntPtr.Zero, ref dwSize, sizeOfHeader);
    1.32 +
    1.33 +                //Allocate a large enough buffer
    1.34 +                 rawInputBuffer = Marshal.AllocHGlobal((int)dwSize);
    1.35 +
    1.36 +                //Now read our RAWINPUT data
    1.37 +                if (Win32.Function.GetRawInputData(aRawInputHandle, Const.RID_INPUT, rawInputBuffer, ref dwSize, (uint)Marshal.SizeOf(typeof(RAWINPUTHEADER))) != dwSize)
    1.38 +                {
    1.39 +                    return false;
    1.40 +                }
    1.41 +
    1.42 +                //Cast our buffer
    1.43 +                aRawInput = (RAWINPUT)Marshal.PtrToStructure(rawInputBuffer, typeof(RAWINPUT));
    1.44 +            }
    1.45 +            catch
    1.46 +            {
    1.47 +                Debug.WriteLine("GetRawInputData failed!");
    1.48 +                success = false;
    1.49 +            }
    1.50 +
    1.51 +            return success;
    1.52 +        }
    1.53 +
    1.54 +        /// <summary>
    1.55 +        /// 
    1.56 +        /// </summary>
    1.57 +        /// <param name="aRawInputHandle"></param>
    1.58 +        /// <param name="aUsagePage"></param>
    1.59 +        /// <param name="aUsage"></param>
    1.60 +        /// <returns></returns>
    1.61 +        public static bool GetDeviceInfo(IntPtr hDevice, ref RID_DEVICE_INFO deviceInfo)
    1.62 +        {
    1.63 +            bool success = true;
    1.64 +            IntPtr deviceInfoBuffer = IntPtr.Zero;
    1.65 +            try
    1.66 +            {
    1.67 +                //Get Device Info
    1.68 +                uint deviceInfoSize = (uint)Marshal.SizeOf(typeof(RID_DEVICE_INFO));
    1.69 +                deviceInfoBuffer = Marshal.AllocHGlobal((int)deviceInfoSize);
    1.70 +
    1.71 +                int res = Win32.Function.GetRawInputDeviceInfo(hDevice, Const.RIDI_DEVICEINFO, deviceInfoBuffer, ref deviceInfoSize);
    1.72 +                if (res <= 0)
    1.73 +                {
    1.74 +                    Debug.WriteLine("WM_INPUT could not read device info: " + Marshal.GetLastWin32Error().ToString());
    1.75 +                    return false;
    1.76 +                }
    1.77 +
    1.78 +                //Cast our buffer
    1.79 +                deviceInfo = (RID_DEVICE_INFO)Marshal.PtrToStructure(deviceInfoBuffer, typeof(RID_DEVICE_INFO));
    1.80 +            }
    1.81 +            catch
    1.82 +            {
    1.83 +                Debug.WriteLine("GetRawInputData failed!");
    1.84 +                success = false;
    1.85 +            }
    1.86 +            finally
    1.87 +            {
    1.88 +                //Always executes, prevents memory leak
    1.89 +                Marshal.FreeHGlobal(deviceInfoBuffer);
    1.90 +            }
    1.91 +
    1.92 +            
    1.93 +            return success;
    1.94 +        }
    1.95 +
    1.96 +    }
    1.97 +}
    1.98 \ No newline at end of file