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