Win32RawInput.cs
author StephaneLenclud
Sun, 15 Feb 2015 22:13:33 +0100
changeset 64 8c2380995bb7
parent 48 1eb878505ae1
child 76 831ebeeecfdf
permissions -rw-r--r--
Adding input capabilities description to device list.
     1 using System;
     2 using System.Runtime.InteropServices;
     3 
     4 namespace Win32
     5 {
     6 
     7     static partial class Function
     8     {
     9         [DllImport("User32.dll", SetLastError = true)]
    10 		public extern static bool RegisterRawInputDevices(RAWINPUTDEVICE[] pRawInputDevice, uint uiNumDevices, uint cbSize);
    11 
    12         [DllImport("User32.dll", SetLastError = true)]
    13 		public extern static uint GetRawInputData(IntPtr hRawInput, uint uiCommand, IntPtr pData, ref uint pcbSize, uint cbSizeHeader);
    14 
    15    		[DllImport("User32.dll", SetLastError=true)]
    16         public extern static int GetRawInputDeviceInfo(IntPtr hDevice, RawInputDeviceInfoType uiCommand, IntPtr pData, ref uint pcbSize);
    17 
    18         [DllImport("user32.dll", SetLastError = true)]
    19         public static extern int GetRawInputDeviceList(
    20             [In, Out] RAWINPUTDEVICELIST[] InputdeviceList,
    21             [In, Out] ref uint puiNumDevices,
    22             [In] uint cbSize);
    23 
    24     }
    25 
    26 
    27     static partial class Macro
    28     {
    29         /// <summary>
    30         /// Retrieves the input code from wParam in WM_INPUT.
    31         /// See RIM_INPUT and RIM_INPUTSINK.
    32         /// </summary>
    33         /// <param name="wParam"></param>
    34         /// <returns></returns>
    35         public static int GET_RAWINPUT_CODE_WPARAM(IntPtr wParam)
    36         {
    37             return (wParam.ToInt32() & 0xff);
    38         }
    39 
    40         public static int GET_DEVICE_LPARAM(IntPtr lParam)
    41         {
    42             return ((ushort)(HIWORD(lParam.ToInt32()) & Const.FAPPCOMMAND_MASK));
    43         }
    44 
    45         public static int HIWORD(int val)
    46         {
    47             return ((val >> 16) & 0xffff);
    48         }
    49 
    50 
    51         //#define HIWORD(l)           ((WORD)((((DWORD_PTR)(l)) >> 16) & 0xffff))
    52         //#define LOWORD(l)           ((WORD)(((DWORD_PTR)(l)) & 0xffff))        
    53         //#define LOBYTE(w)           ((BYTE)(((DWORD_PTR)(w)) & 0xff))
    54         //#define HIBYTE(w)           ((BYTE)((((DWORD_PTR)(w)) >> 8) & 0xff))
    55 
    56         //#define GET_APPCOMMAND_LPARAM(lParam) ((short)(HIWORD(lParam) & ~FAPPCOMMAND_MASK))
    57         //#define GET_DEVICE_LPARAM(lParam)     ((WORD)(HIWORD(lParam) & FAPPCOMMAND_MASK))
    58         //#define GET_MOUSEORKEY_LPARAM         GET_DEVICE_LPARAM
    59         //#define GET_FLAGS_LPARAM(lParam)      (LOWORD(lParam))
    60         //#define GET_KEYSTATE_LPARAM(lParam)   GET_FLAGS_LPARAM(lParam)
    61 
    62     }
    63 
    64 
    65 
    66     static partial class Const
    67     {
    68         /// <summary>
    69         /// Windows Messages
    70         /// </summary>
    71         public const int WM_KEYDOWN = 0x0100;
    72         public const int WM_INPUT = 0x00FF;
    73 
    74 
    75         //
    76         public const int RID_INPUT = 0x10000003;
    77         public const int RID_HEADER = 0x10000005;
    78 
    79         /// <summary>
    80         /// Possible value taken by wParam for WM_INPUT.
    81         /// <para />
    82         /// Input occurred while the application was in the foreground. The application must call DefWindowProc so the system can perform cleanup.
    83         /// </summary>
    84         public const int RIM_INPUT = 0;
    85         /// <summary>
    86         /// Possible value taken by wParam for WM_INPUT.
    87         /// <para />
    88         /// Input occurred while the application was not in the foreground. The application must call DefWindowProc so the system can perform the cleanup.
    89         /// </summary>
    90         public const int RIM_INPUTSINK = 1;
    91 
    92         /// <summary>
    93         /// If set, the application command keys are handled. RIDEV_APPKEYS can be specified only if RIDEV_NOLEGACY is specified for a keyboard device.
    94         /// </summary>        
    95         public const uint RIDEV_APPKEYS = 0x00000400;
    96 
    97         /// <summary>
    98         /// If set, the mouse button click does not activate the other window.
    99         /// </summary>
   100 	    public const uint RIDEV_CAPTUREMOUSE = 0x00000200;
   101         
   102         /// <summary>
   103         /// If set, this enables the caller to receive WM_INPUT_DEVICE_CHANGE notifications for device arrival and device removal.
   104         /// Windows XP:  This flag is not supported until Windows Vista
   105         /// </summary>
   106 	    public const uint RIDEV_DEVNOTIFY = 0x00002000;
   107 
   108         /// <summary>
   109         /// If set, this specifies the top level collections to exclude when reading a complete usage page. This flag only affects a TLC whose usage page is already specified with RIDEV_PAGEONLY.
   110         /// </summary>
   111         public const uint RIDEV_EXCLUDE = 0x00000010;
   112 
   113         /// <summary>
   114         /// If set, this enables the caller to receive input in the background only if the foreground application does not process it. In other words, if the foreground application is not registered for raw input, then the background application that is registered will receive the input.
   115         /// Windows XP:  This flag is not supported until Windows Vista
   116         /// </summary>
   117 	    public const uint RIDEV_EXINPUTSINK = 0x00001000;
   118 
   119         /// <summary>
   120         /// If set, this enables the caller to receive the input even when the caller is not in the foreground. Note that hwndTarget must be specified.
   121         /// </summary>
   122 	    public const uint RIDEV_INPUTSINK = 0x00000100;
   123 
   124 	    /// <summary>
   125 	    /// If set, the application-defined keyboard device hotkeys are not handled. However, the system hotkeys; for example, ALT+TAB and CTRL+ALT+DEL, are still handled. By default, all keyboard hotkeys are handled. RIDEV_NOHOTKEYS can be specified even if RIDEV_NOLEGACY is not specified and hwndTarget is NULL.
   126 	    /// </summary>
   127         public const uint RIDEV_NOHOTKEYS = 0x00000200;
   128 
   129         /// <summary>
   130         /// If set, this prevents any devices specified by usUsagePage or usUsage from generating legacy messages. This is only for the mouse and keyboard. See Remarks.
   131         /// </summary>
   132         public const uint RIDEV_NOLEGACY = 0x00000030;
   133         
   134         /// <summary>
   135         /// If set, this specifies all devices whose top level collection is from the specified usUsagePage. Note that usUsage must be zero. To exclude a particular top level collection, use RIDEV_EXCLUDE.
   136         /// </summary>
   137         public const uint RIDEV_PAGEONLY = 0x00000020;
   138 
   139 	    /// <summary>
   140         /// If set, this removes the top level collection from the inclusion list. This tells the operating system to stop reading from a device which matches the top level collection.
   141 	    /// </summary>
   142         public const uint RIDEV_REMOVE = 0x00000001;
   143 
   144         public const int APPCOMMAND_BROWSER_BACKWARD = 1;
   145         public const int APPCOMMAND_VOLUME_MUTE = 8;
   146         public const int APPCOMMAND_VOLUME_DOWN = 9;
   147         public const int APPCOMMAND_VOLUME_UP = 10;
   148         public const int APPCOMMAND_MEDIA_NEXTTRACK = 11;
   149         public const int APPCOMMAND_MEDIA_PREVIOUSTRACK = 12;
   150         public const int APPCOMMAND_MEDIA_STOP = 13;
   151         public const int APPCOMMAND_MEDIA_PLAY_PAUSE = 14;
   152         public const int APPCOMMAND_MEDIA_PLAY = 46;
   153         public const int APPCOMMAND_MEDIA_PAUSE = 47;
   154         public const int APPCOMMAND_MEDIA_RECORD = 48;
   155         public const int APPCOMMAND_MEDIA_FAST_FORWARD = 49;
   156         public const int APPCOMMAND_MEDIA_REWIND = 50;
   157         public const int APPCOMMAND_MEDIA_CHANNEL_UP = 51;
   158         public const int APPCOMMAND_MEDIA_CHANNEL_DOWN = 52;
   159 
   160         public const int FAPPCOMMAND_MASK = 0xF000;
   161         public const int FAPPCOMMAND_MOUSE = 0x8000;
   162         public const int FAPPCOMMAND_KEY = 0;
   163 		public const int FAPPCOMMAND_OEM = 0x1000;
   164     }
   165 
   166     /// <summary>
   167     /// Introduced this enum for consistency and easy of use.
   168     /// Naming of the Win32 constants were preserved.
   169     /// </summary>
   170     public enum RawInputDeviceType : uint
   171     {
   172         /// <summary>
   173         /// Data comes from a mouse.
   174         /// </summary>
   175         RIM_TYPEMOUSE = 0,
   176         /// <summary>
   177         /// Data comes from a keyboard.
   178         /// </summary>
   179         RIM_TYPEKEYBOARD = 1,
   180         /// <summary>
   181         /// Data comes from an HID that is not a keyboard or a mouse.
   182         /// </summary>
   183         RIM_TYPEHID = 2
   184     }
   185 
   186     /// <summary>
   187     /// Introduced this enum for consistency and easy of use.
   188     /// Naming of the Win32 constants were preserved.
   189     /// </summary>
   190     public enum RawInputDeviceInfoType : uint
   191     {
   192          /// <summary>
   193         /// GetRawInputDeviceInfo pData points to a string that contains the device name.
   194         /// </summary>
   195         RIDI_DEVICENAME = 0x20000007,
   196         /// <summary>
   197         /// GetRawInputDeviceInfo For this uiCommand only, the value in pcbSize is the character count (not the byte count).
   198         /// </summary>
   199         RIDI_DEVICEINFO = 0x2000000b,
   200         /// <summary>
   201         /// GetRawInputDeviceInfo pData points to an RID_DEVICE_INFO structure.
   202         /// </summary>
   203         RIDI_PREPARSEDDATA = 0x20000005
   204     }
   205 
   206 
   207     [StructLayout(LayoutKind.Sequential, Pack = 1)]
   208     public struct RAWINPUTDEVICELIST
   209     {
   210         public IntPtr hDevice;
   211         public RawInputDeviceType dwType;
   212     }
   213 
   214     [StructLayout(LayoutKind.Sequential, Pack = 1)]
   215     public struct RAWINPUTDEVICE
   216     {
   217         [MarshalAs(UnmanagedType.U2)]
   218         public ushort usUsagePage;
   219         [MarshalAs(UnmanagedType.U2)]
   220         public ushort usUsage;
   221         [MarshalAs(UnmanagedType.U4)]
   222         public uint dwFlags;
   223         public IntPtr hwndTarget;
   224     }
   225 
   226 
   227     [StructLayout(LayoutKind.Sequential, Pack = 1)]
   228     public struct RAWINPUTHEADER
   229     {
   230         [MarshalAs(UnmanagedType.U4)]
   231         public RawInputDeviceType dwType;
   232         [MarshalAs(UnmanagedType.U4)]
   233         public int dwSize;
   234         public IntPtr hDevice;
   235         [MarshalAs(UnmanagedType.U4)]
   236         public int wParam;
   237     }
   238 
   239 
   240     [StructLayout(LayoutKind.Sequential, Pack = 1)]
   241     public struct RAWHID
   242     {
   243         [MarshalAs(UnmanagedType.U4)]
   244         public uint dwSizeHid;
   245         [MarshalAs(UnmanagedType.U4)]
   246         public uint dwCount;
   247         //
   248         //BYTE  bRawData[1];
   249     }
   250 
   251 
   252     [StructLayout(LayoutKind.Sequential, Pack = 1)]
   253     public struct BUTTONSSTR
   254     {
   255         [MarshalAs(UnmanagedType.U2)]
   256         public ushort usButtonFlags;
   257         [MarshalAs(UnmanagedType.U2)]
   258         public ushort usButtonData;
   259     }
   260 
   261 
   262     [StructLayout(LayoutKind.Explicit, Pack = 1)]
   263     public struct RAWMOUSE
   264     {
   265         [MarshalAs(UnmanagedType.U2)]
   266         [FieldOffset(0)]
   267         public ushort usFlags;
   268         [MarshalAs(UnmanagedType.U4)]
   269         [FieldOffset(4)]
   270         public uint ulButtons;
   271         [FieldOffset(4)]
   272         public BUTTONSSTR buttonsStr;
   273         [MarshalAs(UnmanagedType.U4)]
   274         [FieldOffset(8)]
   275         public uint ulRawButtons;
   276         [MarshalAs(UnmanagedType.U4)]
   277         [FieldOffset(12)]
   278         public int lLastX;
   279         [MarshalAs(UnmanagedType.U4)]
   280         [FieldOffset(16)]
   281         public int lLastY;
   282         [MarshalAs(UnmanagedType.U4)]
   283         [FieldOffset(20)]
   284         public uint ulExtraInformation;
   285     }
   286 
   287     [StructLayout(LayoutKind.Sequential, Pack = 1)]
   288     public struct RAWKEYBOARD
   289     {
   290         [MarshalAs(UnmanagedType.U2)]
   291         public ushort MakeCode;
   292         [MarshalAs(UnmanagedType.U2)]
   293         public ushort Flags;
   294         [MarshalAs(UnmanagedType.U2)]
   295         public ushort Reserved;
   296         [MarshalAs(UnmanagedType.U2)]
   297         public ushort VKey;
   298         [MarshalAs(UnmanagedType.U4)]
   299         public uint Message;
   300         [MarshalAs(UnmanagedType.U4)]
   301         public uint ExtraInformation;
   302     }
   303 
   304 
   305     [StructLayout(LayoutKind.Explicit, Pack = 1)]
   306     public struct RAWINPUT
   307     {
   308         [FieldOffset(0)]
   309         public RAWINPUTHEADER header;
   310         [FieldOffset(16)]
   311         public RAWMOUSE mouse;
   312         [FieldOffset(16)]
   313         public RAWKEYBOARD keyboard;
   314         [FieldOffset(16)]
   315         public RAWHID hid;
   316     }
   317 
   318 
   319     [StructLayout(LayoutKind.Sequential, Pack = 1)]
   320     public struct RID_DEVICE_INFO_MOUSE
   321     {
   322         public uint dwId;
   323         public uint dwNumberOfButtons;
   324         public uint dwSampleRate;
   325         public bool fHasHorizontalWheel;
   326     }
   327 
   328 
   329     [StructLayout(LayoutKind.Sequential, Pack = 1)]
   330     public struct RID_DEVICE_INFO_KEYBOARD
   331     {
   332         public uint dwType;
   333         public uint dwSubType;
   334         public uint dwKeyboardMode;
   335         public uint dwNumberOfFunctionKeys;
   336         public uint dwNumberOfIndicators;
   337         public uint dwNumberOfKeysTotal;
   338     }
   339 
   340     [StructLayout(LayoutKind.Sequential, Pack = 1)]
   341     public struct RID_DEVICE_INFO_HID
   342     {
   343         public uint dwVendorId;
   344         public uint dwProductId;
   345         public uint dwVersionNumber;
   346         public ushort usUsagePage;
   347         public ushort usUsage;
   348     }
   349 
   350     [StructLayout(LayoutKind.Explicit, Pack = 1)]
   351     public struct RID_DEVICE_INFO
   352     {
   353         [FieldOffset(0)]
   354         public uint cbSize;
   355         [FieldOffset(4)]
   356         [MarshalAsAttribute(UnmanagedType.U4)]
   357         public RawInputDeviceType dwType;
   358         [FieldOffset(8)]
   359         public RID_DEVICE_INFO_MOUSE mouse;
   360         [FieldOffset(8)]
   361         public RID_DEVICE_INFO_KEYBOARD keyboard;
   362         [FieldOffset(8)]
   363         public RID_DEVICE_INFO_HID hid;
   364     }
   365 
   366 
   367 }