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.
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;
sl@15
   181
        public const int FAPPCOMMAND_OEM = 0x1000;
sl@10
   182
sl@9
   183
    }
sl@9
   184
sl@9
   185
sl@9
   186
    [StructLayout(LayoutKind.Sequential, Pack = 1)]
sl@40
   187
    public struct RAWINPUTDEVICE
sl@9
   188
    {
sl@9
   189
        [MarshalAs(UnmanagedType.U2)]
sl@9
   190
        public ushort usUsagePage;
sl@9
   191
        [MarshalAs(UnmanagedType.U2)]
sl@9
   192
        public ushort usUsage;
sl@9
   193
        [MarshalAs(UnmanagedType.U4)]
sl@15
   194
        public uint dwFlags;
sl@9
   195
        public IntPtr hwndTarget;
sl@9
   196
    }
sl@9
   197
sl@9
   198
sl@9
   199
    [StructLayout(LayoutKind.Sequential, Pack = 1)]
sl@40
   200
    public struct RAWINPUTHEADER
sl@9
   201
    {
sl@9
   202
        [MarshalAs(UnmanagedType.U4)]
sl@9
   203
        public int dwType;
sl@9
   204
        [MarshalAs(UnmanagedType.U4)]
sl@9
   205
        public int dwSize;
sl@9
   206
        public IntPtr hDevice;
sl@9
   207
        [MarshalAs(UnmanagedType.U4)]
sl@9
   208
        public int wParam;
sl@9
   209
    }
sl@9
   210
sl@9
   211
sl@9
   212
    [StructLayout(LayoutKind.Sequential, Pack = 1)]
sl@40
   213
    public struct RAWHID
sl@9
   214
    {
sl@9
   215
        [MarshalAs(UnmanagedType.U4)]
sl@17
   216
        public uint dwSizeHid;
sl@9
   217
        [MarshalAs(UnmanagedType.U4)]
sl@17
   218
        public uint dwCount;
sl@9
   219
        //
sl@9
   220
        //BYTE  bRawData[1];
sl@9
   221
    }
sl@9
   222
sl@9
   223
sl@9
   224
    [StructLayout(LayoutKind.Sequential, Pack = 1)]
sl@40
   225
    public struct BUTTONSSTR
sl@9
   226
    {
sl@9
   227
        [MarshalAs(UnmanagedType.U2)]
sl@9
   228
        public ushort usButtonFlags;
sl@9
   229
        [MarshalAs(UnmanagedType.U2)]
sl@9
   230
        public ushort usButtonData;
sl@9
   231
    }
sl@9
   232
sl@9
   233
sl@9
   234
    [StructLayout(LayoutKind.Explicit, Pack = 1)]
sl@40
   235
    public struct RAWMOUSE
sl@9
   236
    {
sl@9
   237
        [MarshalAs(UnmanagedType.U2)]
sl@9
   238
        [FieldOffset(0)]
sl@9
   239
        public ushort usFlags;
sl@9
   240
        [MarshalAs(UnmanagedType.U4)]
sl@9
   241
        [FieldOffset(4)]
sl@9
   242
        public uint ulButtons;
sl@9
   243
        [FieldOffset(4)]
sl@9
   244
        public BUTTONSSTR buttonsStr;
sl@9
   245
        [MarshalAs(UnmanagedType.U4)]
sl@9
   246
        [FieldOffset(8)]
sl@9
   247
        public uint ulRawButtons;
sl@9
   248
        [MarshalAs(UnmanagedType.U4)]
sl@9
   249
        [FieldOffset(12)]
sl@9
   250
        public int lLastX;
sl@9
   251
        [MarshalAs(UnmanagedType.U4)]
sl@9
   252
        [FieldOffset(16)]
sl@9
   253
        public int lLastY;
sl@9
   254
        [MarshalAs(UnmanagedType.U4)]
sl@9
   255
        [FieldOffset(20)]
sl@9
   256
        public uint ulExtraInformation;
sl@9
   257
    }
sl@9
   258
sl@9
   259
    [StructLayout(LayoutKind.Sequential, Pack = 1)]
sl@40
   260
    public struct RAWKEYBOARD
sl@9
   261
    {
sl@9
   262
        [MarshalAs(UnmanagedType.U2)]
sl@9
   263
        public ushort MakeCode;
sl@9
   264
        [MarshalAs(UnmanagedType.U2)]
sl@9
   265
        public ushort Flags;
sl@9
   266
        [MarshalAs(UnmanagedType.U2)]
sl@9
   267
        public ushort Reserved;
sl@9
   268
        [MarshalAs(UnmanagedType.U2)]
sl@9
   269
        public ushort VKey;
sl@9
   270
        [MarshalAs(UnmanagedType.U4)]
sl@9
   271
        public uint Message;
sl@9
   272
        [MarshalAs(UnmanagedType.U4)]
sl@9
   273
        public uint ExtraInformation;
sl@9
   274
    }
sl@9
   275
sl@9
   276
sl@9
   277
    [StructLayout(LayoutKind.Explicit, Pack = 1)]
sl@40
   278
    public struct RAWINPUT
sl@9
   279
    {
sl@9
   280
        [FieldOffset(0)]
sl@9
   281
        public RAWINPUTHEADER header;
sl@9
   282
        [FieldOffset(16)]
sl@9
   283
        public RAWMOUSE mouse;
sl@9
   284
        [FieldOffset(16)]
sl@9
   285
        public RAWKEYBOARD keyboard;
sl@9
   286
        [FieldOffset(16)]
sl@9
   287
        public RAWHID hid;
sl@9
   288
    }
sl@9
   289
sl@9
   290
sl@9
   291
    [StructLayout(LayoutKind.Sequential, Pack = 1)]
sl@40
   292
    public struct RID_DEVICE_INFO_MOUSE
sl@9
   293
    {
sl@9
   294
        public uint dwId;
sl@9
   295
        public uint dwNumberOfButtons;
sl@9
   296
        public uint dwSampleRate;
sl@9
   297
        public bool fHasHorizontalWheel;
sl@9
   298
    }
sl@9
   299
sl@9
   300
sl@9
   301
    [StructLayout(LayoutKind.Sequential, Pack = 1)]
sl@40
   302
    public struct RID_DEVICE_INFO_KEYBOARD
sl@9
   303
    {
sl@9
   304
        public uint dwType;
sl@9
   305
        public uint dwSubType;
sl@9
   306
        public uint dwKeyboardMode;
sl@9
   307
        public uint dwNumberOfFunctionKeys;
sl@9
   308
        public uint dwNumberOfIndicators;
sl@9
   309
        public uint dwNumberOfKeysTotal;
sl@9
   310
    }
sl@9
   311
sl@9
   312
    [StructLayout(LayoutKind.Sequential, Pack = 1)]
sl@40
   313
    public struct RID_DEVICE_INFO_HID
sl@9
   314
    {
sl@9
   315
        public uint dwVendorId;
sl@9
   316
        public uint dwProductId;
sl@9
   317
        public uint dwVersionNumber;
sl@9
   318
        public ushort usUsagePage;
sl@9
   319
        public ushort usUsage;
sl@9
   320
    }
sl@9
   321
sl@9
   322
    [StructLayout(LayoutKind.Explicit, Pack = 1)]
sl@40
   323
    public struct RID_DEVICE_INFO
sl@9
   324
    {
sl@9
   325
        [FieldOffset(0)]
sl@9
   326
        public uint cbSize;
sl@9
   327
        [FieldOffset(4)]
sl@9
   328
        public uint dwType;
sl@9
   329
        [FieldOffset(8)]
sl@9
   330
        public RID_DEVICE_INFO_MOUSE mouse;
sl@9
   331
        [FieldOffset(8)]
sl@9
   332
        public RID_DEVICE_INFO_KEYBOARD keyboard;
sl@9
   333
        [FieldOffset(8)]
sl@9
   334
        public RID_DEVICE_INFO_HID hid;
sl@9
   335
    }
sl@9
   336
sl@9
   337
sl@9
   338
}