Win32RawInput.cs
author sl
Fri, 07 Nov 2014 22:12:22 +0100
changeset 10 17f8421146ba
parent 9 94850bfc12b5
child 15 2044181ade12
permissions -rw-r--r--
More cleanup and re-org.
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@9
    16
		public extern static int GetRawInputDeviceInfo(IntPtr hDevice, uint uiCommand, IntPtr pData, ref uint pcbSize);
sl@9
    17
    }
sl@9
    18
sl@9
    19
    static partial class Const
sl@9
    20
    {
sl@9
    21
        /// <summary>
sl@9
    22
        /// GetRawInputDeviceInfo pData points to a string that contains the device name.
sl@9
    23
        /// </summary>
sl@9
    24
        public const uint RIDI_DEVICENAME = 0x20000007;
sl@9
    25
        /// <summary>
sl@9
    26
        /// GetRawInputDeviceInfo For this uiCommand only, the value in pcbSize is the character count (not the byte count).
sl@9
    27
        /// </summary>
sl@9
    28
        public const uint RIDI_DEVICEINFO = 0x2000000b;
sl@9
    29
        /// <summary>
sl@9
    30
        /// GetRawInputDeviceInfo pData points to an RID_DEVICE_INFO structure.
sl@9
    31
        /// </summary>
sl@9
    32
        public const uint RIDI_PREPARSEDDATA = 0x20000005;
sl@9
    33
sl@9
    34
sl@9
    35
        /// <summary>
sl@9
    36
        /// Data comes from a mouse.
sl@9
    37
        /// </summary>
sl@9
    38
        public const uint RIM_TYPEMOUSE = 0;
sl@9
    39
        /// <summary>
sl@9
    40
        /// Data comes from a keyboard.
sl@9
    41
        /// </summary>
sl@9
    42
        public const uint RIM_TYPEKEYBOARD = 1;
sl@9
    43
        /// <summary>
sl@9
    44
        /// Data comes from an HID that is not a keyboard or a mouse.
sl@9
    45
        /// </summary>
sl@9
    46
        public const uint RIM_TYPEHID = 2;
sl@9
    47
sl@10
    48
        public const int RID_INPUT = 0x10000003;
sl@10
    49
        public const int RID_HEADER = 0x10000005;
sl@10
    50
sl@10
    51
sl@9
    52
    }
sl@9
    53
sl@9
    54
sl@9
    55
    [StructLayout(LayoutKind.Sequential, Pack = 1)]
sl@9
    56
    internal struct RAWINPUTDEVICE
sl@9
    57
    {
sl@9
    58
        [MarshalAs(UnmanagedType.U2)]
sl@9
    59
        public ushort usUsagePage;
sl@9
    60
        [MarshalAs(UnmanagedType.U2)]
sl@9
    61
        public ushort usUsage;
sl@9
    62
        [MarshalAs(UnmanagedType.U4)]
sl@9
    63
        public int dwFlags;
sl@9
    64
        public IntPtr hwndTarget;
sl@9
    65
    }
sl@9
    66
sl@9
    67
sl@9
    68
    [StructLayout(LayoutKind.Sequential, Pack = 1)]
sl@9
    69
    internal struct RAWINPUTHEADER
sl@9
    70
    {
sl@9
    71
        [MarshalAs(UnmanagedType.U4)]
sl@9
    72
        public int dwType;
sl@9
    73
        [MarshalAs(UnmanagedType.U4)]
sl@9
    74
        public int dwSize;
sl@9
    75
        public IntPtr hDevice;
sl@9
    76
        [MarshalAs(UnmanagedType.U4)]
sl@9
    77
        public int wParam;
sl@9
    78
    }
sl@9
    79
sl@9
    80
sl@9
    81
    [StructLayout(LayoutKind.Sequential, Pack = 1)]
sl@9
    82
    internal struct RAWHID
sl@9
    83
    {
sl@9
    84
        [MarshalAs(UnmanagedType.U4)]
sl@9
    85
        public int dwSizeHid;
sl@9
    86
        [MarshalAs(UnmanagedType.U4)]
sl@9
    87
        public int dwCount;
sl@9
    88
        //
sl@9
    89
        //BYTE  bRawData[1];
sl@9
    90
    }
sl@9
    91
sl@9
    92
sl@9
    93
    [StructLayout(LayoutKind.Sequential, Pack = 1)]
sl@9
    94
    internal struct BUTTONSSTR
sl@9
    95
    {
sl@9
    96
        [MarshalAs(UnmanagedType.U2)]
sl@9
    97
        public ushort usButtonFlags;
sl@9
    98
        [MarshalAs(UnmanagedType.U2)]
sl@9
    99
        public ushort usButtonData;
sl@9
   100
    }
sl@9
   101
sl@9
   102
sl@9
   103
    [StructLayout(LayoutKind.Explicit, Pack = 1)]
sl@9
   104
    internal struct RAWMOUSE
sl@9
   105
    {
sl@9
   106
        [MarshalAs(UnmanagedType.U2)]
sl@9
   107
        [FieldOffset(0)]
sl@9
   108
        public ushort usFlags;
sl@9
   109
        [MarshalAs(UnmanagedType.U4)]
sl@9
   110
        [FieldOffset(4)]
sl@9
   111
        public uint ulButtons;
sl@9
   112
        [FieldOffset(4)]
sl@9
   113
        public BUTTONSSTR buttonsStr;
sl@9
   114
        [MarshalAs(UnmanagedType.U4)]
sl@9
   115
        [FieldOffset(8)]
sl@9
   116
        public uint ulRawButtons;
sl@9
   117
        [MarshalAs(UnmanagedType.U4)]
sl@9
   118
        [FieldOffset(12)]
sl@9
   119
        public int lLastX;
sl@9
   120
        [MarshalAs(UnmanagedType.U4)]
sl@9
   121
        [FieldOffset(16)]
sl@9
   122
        public int lLastY;
sl@9
   123
        [MarshalAs(UnmanagedType.U4)]
sl@9
   124
        [FieldOffset(20)]
sl@9
   125
        public uint ulExtraInformation;
sl@9
   126
    }
sl@9
   127
sl@9
   128
    [StructLayout(LayoutKind.Sequential, Pack = 1)]
sl@9
   129
    internal struct RAWKEYBOARD
sl@9
   130
    {
sl@9
   131
        [MarshalAs(UnmanagedType.U2)]
sl@9
   132
        public ushort MakeCode;
sl@9
   133
        [MarshalAs(UnmanagedType.U2)]
sl@9
   134
        public ushort Flags;
sl@9
   135
        [MarshalAs(UnmanagedType.U2)]
sl@9
   136
        public ushort Reserved;
sl@9
   137
        [MarshalAs(UnmanagedType.U2)]
sl@9
   138
        public ushort VKey;
sl@9
   139
        [MarshalAs(UnmanagedType.U4)]
sl@9
   140
        public uint Message;
sl@9
   141
        [MarshalAs(UnmanagedType.U4)]
sl@9
   142
        public uint ExtraInformation;
sl@9
   143
    }
sl@9
   144
sl@9
   145
sl@9
   146
    [StructLayout(LayoutKind.Explicit, Pack = 1)]
sl@9
   147
    internal struct RAWINPUT
sl@9
   148
    {
sl@9
   149
        [FieldOffset(0)]
sl@9
   150
        public RAWINPUTHEADER header;
sl@9
   151
        [FieldOffset(16)]
sl@9
   152
        public RAWMOUSE mouse;
sl@9
   153
        [FieldOffset(16)]
sl@9
   154
        public RAWKEYBOARD keyboard;
sl@9
   155
        [FieldOffset(16)]
sl@9
   156
        public RAWHID hid;
sl@9
   157
    }
sl@9
   158
sl@9
   159
sl@9
   160
    [StructLayout(LayoutKind.Sequential, Pack = 1)]
sl@9
   161
    internal struct RID_DEVICE_INFO_MOUSE
sl@9
   162
    {
sl@9
   163
        public uint dwId;
sl@9
   164
        public uint dwNumberOfButtons;
sl@9
   165
        public uint dwSampleRate;
sl@9
   166
        public bool fHasHorizontalWheel;
sl@9
   167
    }
sl@9
   168
sl@9
   169
sl@9
   170
    [StructLayout(LayoutKind.Sequential, Pack = 1)]
sl@9
   171
    internal struct RID_DEVICE_INFO_KEYBOARD
sl@9
   172
    {
sl@9
   173
        public uint dwType;
sl@9
   174
        public uint dwSubType;
sl@9
   175
        public uint dwKeyboardMode;
sl@9
   176
        public uint dwNumberOfFunctionKeys;
sl@9
   177
        public uint dwNumberOfIndicators;
sl@9
   178
        public uint dwNumberOfKeysTotal;
sl@9
   179
    }
sl@9
   180
sl@9
   181
    [StructLayout(LayoutKind.Sequential, Pack = 1)]
sl@9
   182
    internal struct RID_DEVICE_INFO_HID
sl@9
   183
    {
sl@9
   184
        public uint dwVendorId;
sl@9
   185
        public uint dwProductId;
sl@9
   186
        public uint dwVersionNumber;
sl@9
   187
        public ushort usUsagePage;
sl@9
   188
        public ushort usUsage;
sl@9
   189
    }
sl@9
   190
sl@9
   191
    [StructLayout(LayoutKind.Explicit, Pack = 1)]
sl@9
   192
    internal struct RID_DEVICE_INFO
sl@9
   193
    {
sl@9
   194
        [FieldOffset(0)]
sl@9
   195
        public uint cbSize;
sl@9
   196
        [FieldOffset(4)]
sl@9
   197
        public uint dwType;
sl@9
   198
        [FieldOffset(8)]
sl@9
   199
        public RID_DEVICE_INFO_MOUSE mouse;
sl@9
   200
        [FieldOffset(8)]
sl@9
   201
        public RID_DEVICE_INFO_KEYBOARD keyboard;
sl@9
   202
        [FieldOffset(8)]
sl@9
   203
        public RID_DEVICE_INFO_HID hid;
sl@9
   204
    }
sl@9
   205
sl@9
   206
sl@9
   207
}