Win32RawInput.cs
author sl
Sat, 06 Dec 2014 02:46:46 +0100
changeset 22 8b2a3da54187
parent 17 8f7e35c3bfd1
child 23 743cadfacda0
permissions -rw-r--r--
Adding method to get the device name.
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@22
    16
		public extern static int GetRawInputDeviceInfoW(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_APPCOMMAND_LPARAM(IntPtr lParam)
sl@15
    34
        {
sl@15
    35
            return ((short)HIWORD(lParam.ToInt32()) & ~Const.FAPPCOMMAND_MASK);
sl@15
    36
        }
sl@15
    37
sl@15
    38
        public static int GET_DEVICE_LPARAM(IntPtr lParam)
sl@15
    39
        {
sl@15
    40
            return ((ushort)(HIWORD(lParam.ToInt32()) & Const.FAPPCOMMAND_MASK));
sl@15
    41
        }
sl@15
    42
sl@15
    43
        public static int HIWORD(int val)
sl@15
    44
        {
sl@15
    45
            return ((val >> 16) & 0xffff);
sl@15
    46
        }
sl@15
    47
sl@15
    48
sl@15
    49
        //#define HIWORD(l)           ((WORD)((((DWORD_PTR)(l)) >> 16) & 0xffff))
sl@15
    50
        //#define LOWORD(l)           ((WORD)(((DWORD_PTR)(l)) & 0xffff))        
sl@15
    51
        //#define LOBYTE(w)           ((BYTE)(((DWORD_PTR)(w)) & 0xff))
sl@15
    52
        //#define HIBYTE(w)           ((BYTE)((((DWORD_PTR)(w)) >> 8) & 0xff))
sl@15
    53
sl@15
    54
        //#define GET_APPCOMMAND_LPARAM(lParam) ((short)(HIWORD(lParam) & ~FAPPCOMMAND_MASK))
sl@15
    55
        //#define GET_DEVICE_LPARAM(lParam)     ((WORD)(HIWORD(lParam) & FAPPCOMMAND_MASK))
sl@15
    56
        //#define GET_MOUSEORKEY_LPARAM         GET_DEVICE_LPARAM
sl@15
    57
        //#define GET_FLAGS_LPARAM(lParam)      (LOWORD(lParam))
sl@15
    58
        //#define GET_KEYSTATE_LPARAM(lParam)   GET_FLAGS_LPARAM(lParam)
sl@15
    59
sl@15
    60
    }
sl@15
    61
sl@15
    62
sl@15
    63
sl@9
    64
    static partial class Const
sl@9
    65
    {
sl@9
    66
        /// <summary>
sl@15
    67
        /// Windows Messages
sl@15
    68
        /// </summary>
sl@15
    69
        public const int WM_KEYDOWN = 0x0100;
sl@15
    70
        public const int WM_APPCOMMAND = 0x0319;
sl@15
    71
        public const int WM_INPUT = 0x00FF;
sl@15
    72
sl@15
    73
        /// <summary>
sl@9
    74
        /// GetRawInputDeviceInfo pData points to a string that contains the device name.
sl@9
    75
        /// </summary>
sl@9
    76
        public const uint RIDI_DEVICENAME = 0x20000007;
sl@9
    77
        /// <summary>
sl@9
    78
        /// GetRawInputDeviceInfo For this uiCommand only, the value in pcbSize is the character count (not the byte count).
sl@9
    79
        /// </summary>
sl@9
    80
        public const uint RIDI_DEVICEINFO = 0x2000000b;
sl@9
    81
        /// <summary>
sl@9
    82
        /// GetRawInputDeviceInfo pData points to an RID_DEVICE_INFO structure.
sl@9
    83
        /// </summary>
sl@9
    84
        public const uint RIDI_PREPARSEDDATA = 0x20000005;
sl@9
    85
sl@9
    86
sl@9
    87
        /// <summary>
sl@9
    88
        /// Data comes from a mouse.
sl@9
    89
        /// </summary>
sl@9
    90
        public const uint RIM_TYPEMOUSE = 0;
sl@9
    91
        /// <summary>
sl@9
    92
        /// Data comes from a keyboard.
sl@9
    93
        /// </summary>
sl@9
    94
        public const uint RIM_TYPEKEYBOARD = 1;
sl@9
    95
        /// <summary>
sl@9
    96
        /// Data comes from an HID that is not a keyboard or a mouse.
sl@9
    97
        /// </summary>
sl@9
    98
        public const uint RIM_TYPEHID = 2;
sl@9
    99
sl@10
   100
        public const int RID_INPUT = 0x10000003;
sl@10
   101
        public const int RID_HEADER = 0x10000005;
sl@10
   102
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 in the foreground. The application must call DefWindowProc so the system can perform cleanup.
sl@15
   107
        /// </summary>
sl@15
   108
        public const int RIM_INPUT = 0;
sl@15
   109
        /// <summary>
sl@15
   110
        /// Possible value taken by wParam for WM_INPUT.
sl@15
   111
        /// <para />
sl@15
   112
        /// Input occurred while the application was not in the foreground. The application must call DefWindowProc so the system can perform the cleanup.
sl@15
   113
        /// </summary>
sl@15
   114
        public const int RIM_INPUTSINK = 1;
sl@15
   115
sl@15
   116
        /// <summary>
sl@15
   117
        /// 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
   118
        /// </summary>        
sl@15
   119
        public const uint RIDEV_APPKEYS = 0x00000400;
sl@15
   120
sl@15
   121
        /// <summary>
sl@15
   122
        /// If set, the mouse button click does not activate the other window.
sl@15
   123
        /// </summary>
sl@15
   124
	    public const uint RIDEV_CAPTUREMOUSE = 0x00000200;
sl@15
   125
        
sl@15
   126
        /// <summary>
sl@15
   127
        /// If set, this enables the caller to receive WM_INPUT_DEVICE_CHANGE notifications for device arrival and device removal.
sl@15
   128
        /// Windows XP:  This flag is not supported until Windows Vista
sl@15
   129
        /// </summary>
sl@15
   130
	    public const uint RIDEV_DEVNOTIFY = 0x00002000;
sl@15
   131
sl@15
   132
        /// <summary>
sl@15
   133
        /// 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
   134
        /// </summary>
sl@15
   135
        public const uint RIDEV_EXCLUDE = 0x00000010;
sl@15
   136
sl@15
   137
        /// <summary>
sl@15
   138
        /// 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
   139
        /// Windows XP:  This flag is not supported until Windows Vista
sl@15
   140
        /// </summary>
sl@15
   141
	    public const uint RIDEV_EXINPUTSINK = 0x00001000;
sl@15
   142
sl@15
   143
        /// <summary>
sl@15
   144
        /// 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
   145
        /// </summary>
sl@15
   146
	    public const uint RIDEV_INPUTSINK = 0x00000100;
sl@15
   147
sl@15
   148
	    /// <summary>
sl@15
   149
	    /// 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
   150
	    /// </summary>
sl@15
   151
        public const uint RIDEV_NOHOTKEYS = 0x00000200;
sl@15
   152
sl@15
   153
        /// <summary>
sl@15
   154
        /// 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
   155
        /// </summary>
sl@15
   156
        public const uint RIDEV_NOLEGACY = 0x00000030;
sl@15
   157
        
sl@15
   158
        /// <summary>
sl@15
   159
        /// 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
   160
        /// </summary>
sl@15
   161
        public const uint RIDEV_PAGEONLY = 0x00000020;
sl@15
   162
sl@15
   163
	    /// <summary>
sl@15
   164
        /// 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
   165
	    /// </summary>
sl@15
   166
        public const uint RIDEV_REMOVE = 0x00000001;
sl@15
   167
sl@15
   168
        public const int APPCOMMAND_BROWSER_BACKWARD = 1;
sl@15
   169
        public const int APPCOMMAND_VOLUME_MUTE = 8;
sl@15
   170
        public const int APPCOMMAND_VOLUME_DOWN = 9;
sl@15
   171
        public const int APPCOMMAND_VOLUME_UP = 10;
sl@15
   172
        public const int APPCOMMAND_MEDIA_NEXTTRACK = 11;
sl@15
   173
        public const int APPCOMMAND_MEDIA_PREVIOUSTRACK = 12;
sl@15
   174
        public const int APPCOMMAND_MEDIA_STOP = 13;
sl@15
   175
        public const int APPCOMMAND_MEDIA_PLAY_PAUSE = 14;
sl@15
   176
        public const int APPCOMMAND_MEDIA_PLAY = 46;
sl@15
   177
        public const int APPCOMMAND_MEDIA_PAUSE = 47;
sl@15
   178
        public const int APPCOMMAND_MEDIA_RECORD = 48;
sl@15
   179
        public const int APPCOMMAND_MEDIA_FAST_FORWARD = 49;
sl@15
   180
        public const int APPCOMMAND_MEDIA_REWIND = 50;
sl@15
   181
        public const int APPCOMMAND_MEDIA_CHANNEL_UP = 51;
sl@15
   182
        public const int APPCOMMAND_MEDIA_CHANNEL_DOWN = 52;
sl@15
   183
sl@15
   184
        public const int FAPPCOMMAND_MASK = 0xF000;
sl@15
   185
        public const int FAPPCOMMAND_MOUSE = 0x8000;
sl@15
   186
        public const int FAPPCOMMAND_KEY = 0;
sl@15
   187
        public const int FAPPCOMMAND_OEM = 0x1000;
sl@10
   188
sl@9
   189
    }
sl@9
   190
sl@9
   191
sl@9
   192
    [StructLayout(LayoutKind.Sequential, Pack = 1)]
sl@9
   193
    internal 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@9
   206
    internal 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@9
   219
    internal 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@9
   231
    internal 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@9
   241
    internal 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@9
   266
    internal 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@9
   284
    internal 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@9
   298
    internal 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@9
   308
    internal 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@9
   319
    internal 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@9
   329
    internal 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
}