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