# HG changeset patch # User StephaneLenclud # Date 1424020541 -3600 # Node ID 687cace560d276a561fd2ee4b785f5a1bdcb593c # Parent 89dfad9633b2768c174468420c2cd68a59c26077 Adding most basic raw device list. diff -r 89dfad9633b2 -r 687cace560d2 HidDevice.cs --- a/HidDevice.cs Sun Feb 15 13:30:54 2015 +0100 +++ b/HidDevice.cs Sun Feb 15 18:15:41 2015 +0100 @@ -10,6 +10,7 @@ { /// /// Represent a HID device. + /// Rename to RawInputDevice? /// public class HidDevice: IDisposable { @@ -76,10 +77,10 @@ iInputValueCapabilities = null; //Fetch various information defining the given HID device - Name = Win32.Utils.RawInput.GetDeviceName(hRawInputDevice); + Name = Win32.RawInput.GetDeviceName(hRawInputDevice); //Get our HID descriptor pre-parsed data - PreParsedData = Win32.Utils.RawInput.GetPreParsedData(hRawInputDevice); + PreParsedData = Win32.RawInput.GetPreParsedData(hRawInputDevice); if (PreParsedData == IntPtr.Zero) { throw new Exception("HidDevice: GetPreParsedData failed: " + Marshal.GetLastWin32Error().ToString()); @@ -87,7 +88,7 @@ //Fetch device info iInfo = new RID_DEVICE_INFO(); - if (!Win32.Utils.RawInput.GetDeviceInfo(hRawInputDevice, ref iInfo)) + if (!Win32.RawInput.GetDeviceInfo(hRawInputDevice, ref iInfo)) { throw new Exception("HidDevice: GetDeviceInfo failed: " + Marshal.GetLastWin32Error().ToString()); } diff -r 89dfad9633b2 -r 687cace560d2 HidEvent.cs --- a/HidEvent.cs Sun Feb 15 13:30:54 2015 +0100 +++ b/HidEvent.cs Sun Feb 15 18:15:41 2015 +0100 @@ -106,7 +106,7 @@ { //Fetch raw input RAWINPUT rawInput = new RAWINPUT(); - if (!Win32.Utils.RawInput.GetRawInputData(aMessage.LParam, ref rawInput, ref rawInputBuffer)) + if (!Win32.RawInput.GetRawInputData(aMessage.LParam, ref rawInput, ref rawInputBuffer)) { return; } @@ -115,7 +115,7 @@ //Get various information about this HID device Device = new Hid.HidDevice(rawInput.header.hDevice); - if (rawInput.header.dwType == Const.RIM_TYPEHID) //Check that our raw input is HID + if (rawInput.header.dwType == Win32.RawInputDeviceType.RIM_TYPEHID) //Check that our raw input is HID { IsGeneric = true; @@ -191,14 +191,14 @@ } } } - else if (rawInput.header.dwType == Const.RIM_TYPEMOUSE) + else if (rawInput.header.dwType == RawInputDeviceType.RIM_TYPEMOUSE) { IsMouse = true; Debug.WriteLine("WM_INPUT source device is Mouse."); // do mouse handling... } - else if (rawInput.header.dwType == Const.RIM_TYPEKEYBOARD) + else if (rawInput.header.dwType == RawInputDeviceType.RIM_TYPEKEYBOARD) { IsKeyboard = true; diff -r 89dfad9633b2 -r 687cace560d2 MainForm.Designer.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MainForm.Designer.cs Sun Feb 15 18:15:41 2015 +0100 @@ -0,0 +1,179 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace RemoteControlSample +{ + public partial class MainForm + { + #region Windows Form Designer generated code + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.labelButtonName = new System.Windows.Forms.Label(); + this.labelDeviceName = new System.Windows.Forms.Label(); + this.buttonClear = new System.Windows.Forms.Button(); + this.tabControl = new System.Windows.Forms.TabControl(); + this.tabPageMessages = new System.Windows.Forms.TabPage(); + this.listViewEvents = new System.Windows.Forms.ListView(); + this.columnHeaderUsages = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); + this.columnHeaderInputReport = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); + this.columnHeaderUsagePage = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); + this.columnHeaderUsageCollection = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); + this.columnHeaderRepeat = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); + this.columnHeaderTime = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); + this.tabPageDevices = new System.Windows.Forms.TabPage(); + this.treeViewDevices = new System.Windows.Forms.TreeView(); + this.tabControl.SuspendLayout(); + this.tabPageMessages.SuspendLayout(); + this.tabPageDevices.SuspendLayout(); + this.SuspendLayout(); + // + // labelButtonName + // + this.labelButtonName.AutoSize = true; + this.labelButtonName.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.labelButtonName.Location = new System.Drawing.Point(788, 150); + this.labelButtonName.Name = "labelButtonName"; + this.labelButtonName.Size = new System.Drawing.Size(103, 20); + this.labelButtonName.TabIndex = 0; + this.labelButtonName.Text = "Button Name"; + // + // labelDeviceName + // + this.labelDeviceName.AutoSize = true; + this.labelDeviceName.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.labelDeviceName.Location = new System.Drawing.Point(788, 130); + this.labelDeviceName.Name = "labelDeviceName"; + this.labelDeviceName.Size = new System.Drawing.Size(103, 20); + this.labelDeviceName.TabIndex = 1; + this.labelDeviceName.Text = "Device Name"; + // + // buttonClear + // + this.buttonClear.Location = new System.Drawing.Point(816, 104); + this.buttonClear.Name = "buttonClear"; + this.buttonClear.Size = new System.Drawing.Size(75, 23); + this.buttonClear.TabIndex = 3; + this.buttonClear.Text = "Clear"; + this.buttonClear.UseVisualStyleBackColor = true; + this.buttonClear.Click += new System.EventHandler(this.buttonClear_Click); + // + // tabControl + // + this.tabControl.Controls.Add(this.tabPageMessages); + this.tabControl.Controls.Add(this.tabPageDevices); + this.tabControl.Location = new System.Drawing.Point(12, 12); + this.tabControl.Name = "tabControl"; + this.tabControl.SelectedIndex = 0; + this.tabControl.Size = new System.Drawing.Size(902, 514); + this.tabControl.TabIndex = 4; + // + // tabPageMessages + // + this.tabPageMessages.Controls.Add(this.listViewEvents); + this.tabPageMessages.Controls.Add(this.buttonClear); + this.tabPageMessages.Controls.Add(this.labelDeviceName); + this.tabPageMessages.Controls.Add(this.labelButtonName); + this.tabPageMessages.Location = new System.Drawing.Point(4, 22); + this.tabPageMessages.Name = "tabPageMessages"; + this.tabPageMessages.Padding = new System.Windows.Forms.Padding(3); + this.tabPageMessages.Size = new System.Drawing.Size(894, 488); + this.tabPageMessages.TabIndex = 0; + this.tabPageMessages.Text = "Messages"; + this.tabPageMessages.UseVisualStyleBackColor = true; + // + // listViewEvents + // + this.listViewEvents.Alignment = System.Windows.Forms.ListViewAlignment.Left; + this.listViewEvents.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) + | System.Windows.Forms.AnchorStyles.Left))); + this.listViewEvents.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; + this.listViewEvents.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] { + this.columnHeaderUsages, + this.columnHeaderInputReport, + this.columnHeaderUsagePage, + this.columnHeaderUsageCollection, + this.columnHeaderRepeat, + this.columnHeaderTime}); + this.listViewEvents.GridLines = true; + this.listViewEvents.Location = new System.Drawing.Point(8, 6); + this.listViewEvents.Name = "listViewEvents"; + this.listViewEvents.Size = new System.Drawing.Size(744, 474); + this.listViewEvents.TabIndex = 3; + this.listViewEvents.UseCompatibleStateImageBehavior = false; + this.listViewEvents.View = System.Windows.Forms.View.Details; + // + // columnHeaderUsages + // + this.columnHeaderUsages.Text = "Usages"; + this.columnHeaderUsages.Width = 180; + // + // columnHeaderInputReport + // + this.columnHeaderInputReport.Text = "Input Report"; + this.columnHeaderInputReport.Width = 176; + // + // columnHeaderUsagePage + // + this.columnHeaderUsagePage.Text = "Usage Page"; + this.columnHeaderUsagePage.Width = 87; + // + // columnHeaderUsageCollection + // + this.columnHeaderUsageCollection.Text = "Usage Collection"; + this.columnHeaderUsageCollection.Width = 134; + // + // columnHeaderRepeat + // + this.columnHeaderRepeat.Text = "Repeat"; + this.columnHeaderRepeat.Width = 68; + // + // columnHeaderTime + // + this.columnHeaderTime.Text = "Time"; + this.columnHeaderTime.Width = 76; + // + // tabPageDevices + // + this.tabPageDevices.Controls.Add(this.treeViewDevices); + this.tabPageDevices.Location = new System.Drawing.Point(4, 22); + this.tabPageDevices.Name = "tabPageDevices"; + this.tabPageDevices.Padding = new System.Windows.Forms.Padding(3); + this.tabPageDevices.Size = new System.Drawing.Size(918, 512); + this.tabPageDevices.TabIndex = 1; + this.tabPageDevices.Text = "Devices"; + this.tabPageDevices.UseVisualStyleBackColor = true; + // + // treeViewDevices + // + this.treeViewDevices.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) + | System.Windows.Forms.AnchorStyles.Left))); + this.treeViewDevices.Location = new System.Drawing.Point(8, 6); + this.treeViewDevices.Name = "treeViewDevices"; + this.treeViewDevices.Size = new System.Drawing.Size(713, 498); + this.treeViewDevices.TabIndex = 0; + // + // MainForm + // + this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); + this.BackColor = System.Drawing.SystemColors.Control; + this.ClientSize = new System.Drawing.Size(926, 538); + this.Controls.Add(this.tabControl); + this.Name = "MainForm"; + this.Text = "Remote Control Sample"; + this.Load += new System.EventHandler(this.MainForm_Load); + this.tabControl.ResumeLayout(false); + this.tabPageMessages.ResumeLayout(false); + this.tabPageMessages.PerformLayout(); + this.tabPageDevices.ResumeLayout(false); + this.ResumeLayout(false); + + } + #endregion Windows Form Designer generated code + + } +} diff -r 89dfad9633b2 -r 687cace560d2 MainForm.cs --- a/MainForm.cs Sun Feb 15 13:30:54 2015 +0100 +++ b/MainForm.cs Sun Feb 15 18:15:41 2015 +0100 @@ -11,7 +11,7 @@ /// /// Summary description for Form1. /// - public class MainForm : System.Windows.Forms.Form + public partial class MainForm : System.Windows.Forms.Form { /// /// Required designer variable. @@ -20,14 +20,18 @@ private RemoteControlDevice _remote; private Label labelButtonName; private Label labelDeviceName; + private Button buttonClear; + private TabControl tabControl; + private TabPage tabPageMessages; private ListView listViewEvents; private ColumnHeader columnHeaderUsages; + private ColumnHeader columnHeaderInputReport; private ColumnHeader columnHeaderUsagePage; private ColumnHeader columnHeaderUsageCollection; private ColumnHeader columnHeaderRepeat; private ColumnHeader columnHeaderTime; - private Button buttonClear; - private ColumnHeader columnHeaderInputReport; + private TabPage tabPageDevices; + private TreeView treeViewDevices; private Timer _timer; public delegate void OnHidEventDelegate(object aSender, Hid.HidEvent aHidEvent); @@ -60,123 +64,6 @@ base.Dispose( disposing ); } - #region Windows Form Designer generated code - /// - /// Required method for Designer support - do not modify - /// the contents of this method with the code editor. - /// - private void InitializeComponent() - { - this.labelButtonName = new System.Windows.Forms.Label(); - this.labelDeviceName = new System.Windows.Forms.Label(); - this.listViewEvents = new System.Windows.Forms.ListView(); - this.columnHeaderUsages = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); - this.columnHeaderUsagePage = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); - this.columnHeaderUsageCollection = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); - this.columnHeaderRepeat = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); - this.columnHeaderTime = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); - this.buttonClear = new System.Windows.Forms.Button(); - this.columnHeaderInputReport = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); - this.SuspendLayout(); - // - // labelButtonName - // - this.labelButtonName.AutoSize = true; - this.labelButtonName.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); - this.labelButtonName.Location = new System.Drawing.Point(811, 55); - this.labelButtonName.Name = "labelButtonName"; - this.labelButtonName.Size = new System.Drawing.Size(103, 20); - this.labelButtonName.TabIndex = 0; - this.labelButtonName.Text = "Button Name"; - // - // labelDeviceName - // - this.labelDeviceName.AutoSize = true; - this.labelDeviceName.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); - this.labelDeviceName.Location = new System.Drawing.Point(811, 35); - this.labelDeviceName.Name = "labelDeviceName"; - this.labelDeviceName.Size = new System.Drawing.Size(103, 20); - this.labelDeviceName.TabIndex = 1; - this.labelDeviceName.Text = "Device Name"; - // - // listViewEvents - // - this.listViewEvents.Alignment = System.Windows.Forms.ListViewAlignment.Left; - this.listViewEvents.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) - | System.Windows.Forms.AnchorStyles.Left))); - this.listViewEvents.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; - this.listViewEvents.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] { - this.columnHeaderUsages, - this.columnHeaderInputReport, - this.columnHeaderUsagePage, - this.columnHeaderUsageCollection, - this.columnHeaderRepeat, - this.columnHeaderTime}); - this.listViewEvents.GridLines = true; - this.listViewEvents.Location = new System.Drawing.Point(12, 12); - this.listViewEvents.Name = "listViewEvents"; - this.listViewEvents.Size = new System.Drawing.Size(766, 369); - this.listViewEvents.TabIndex = 2; - this.listViewEvents.UseCompatibleStateImageBehavior = false; - this.listViewEvents.View = System.Windows.Forms.View.Details; - // - // columnHeaderUsages - // - this.columnHeaderUsages.Text = "Usages"; - this.columnHeaderUsages.Width = 180; - // - // columnHeaderUsagePage - // - this.columnHeaderUsagePage.Text = "Usage Page"; - this.columnHeaderUsagePage.Width = 87; - // - // columnHeaderUsageCollection - // - this.columnHeaderUsageCollection.Text = "Usage Collection"; - this.columnHeaderUsageCollection.Width = 134; - // - // columnHeaderRepeat - // - this.columnHeaderRepeat.Text = "Repeat"; - this.columnHeaderRepeat.Width = 68; - // - // columnHeaderTime - // - this.columnHeaderTime.Text = "Time"; - this.columnHeaderTime.Width = 76; - // - // buttonClear - // - this.buttonClear.Location = new System.Drawing.Point(839, 9); - this.buttonClear.Name = "buttonClear"; - this.buttonClear.Size = new System.Drawing.Size(75, 23); - this.buttonClear.TabIndex = 3; - this.buttonClear.Text = "Clear"; - this.buttonClear.UseVisualStyleBackColor = true; - this.buttonClear.Click += new System.EventHandler(this.buttonClear_Click); - // - // columnHeaderInputReport - // - this.columnHeaderInputReport.Text = "Input Report"; - this.columnHeaderInputReport.Width = 176; - // - // MainForm - // - this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); - this.BackColor = System.Drawing.SystemColors.Control; - this.ClientSize = new System.Drawing.Size(926, 393); - this.Controls.Add(this.buttonClear); - this.Controls.Add(this.listViewEvents); - this.Controls.Add(this.labelDeviceName); - this.Controls.Add(this.labelButtonName); - this.Name = "MainForm"; - this.Text = "Remote Control Sample"; - this.Load += new System.EventHandler(this.Form1_Load); - this.ResumeLayout(false); - this.PerformLayout(); - - } - #endregion Windows Form Designer generated code /// /// The main entry point for the application. @@ -184,14 +71,19 @@ [STAThread] static void Main() { + Application.EnableVisualStyles(); Application.Run(new MainForm()); } - private void Form1_Load(object sender, System.EventArgs e) + private void MainForm_Load(object sender, System.EventArgs e) { _remote = new RemoteControlDevice(this.Handle); _remote.ButtonPressed += new Devices.RemoteControl.RemoteControlDevice.RemoteControlDeviceEventHandler(_remote_ButtonPressed); - _remote.iHidHandler.OnHidEvent += HandleHidEventThreadSafe; + _remote.iHidHandler.OnHidEvent += HandleHidEventThreadSafe; + + // + Win32.RawInput.PopulateDeviceList(treeViewDevices); + } public void HandleHidEventThreadSafe(object aSender, Hid.HidEvent aHidEvent) diff -r 89dfad9633b2 -r 687cace560d2 RawInput.cs --- a/RawInput.cs Sun Feb 15 13:30:54 2015 +0100 +++ b/RawInput.cs Sun Feb 15 18:15:41 2015 +0100 @@ -1,9 +1,10 @@ using System; using System.Runtime.InteropServices; using System.Diagnostics; +using System.Windows.Forms; -namespace Win32.Utils +namespace Win32 { /// /// Provide some utility functions for raw input handling. @@ -67,7 +68,7 @@ uint deviceInfoSize = (uint)Marshal.SizeOf(typeof(RID_DEVICE_INFO)); deviceInfoBuffer = Marshal.AllocHGlobal((int)deviceInfoSize); - int res = Win32.Function.GetRawInputDeviceInfo(hDevice, Const.RIDI_DEVICEINFO, deviceInfoBuffer, ref deviceInfoSize); + int res = Win32.Function.GetRawInputDeviceInfo(hDevice, Win32.RawInputDeviceInfoType.RIDI_DEVICEINFO, deviceInfoBuffer, ref deviceInfoSize); if (res <= 0) { Debug.WriteLine("WM_INPUT could not read device info: " + Marshal.GetLastWin32Error().ToString()); @@ -99,8 +100,8 @@ /// public static IntPtr GetPreParsedData(IntPtr hDevice) { - uint ppDataSize = 256; - int result = Win32.Function.GetRawInputDeviceInfo(hDevice, Win32.Const.RIDI_PREPARSEDDATA, IntPtr.Zero, ref ppDataSize); + uint ppDataSize = 0; + int result = Win32.Function.GetRawInputDeviceInfo(hDevice, RawInputDeviceInfoType.RIDI_PREPARSEDDATA, IntPtr.Zero, ref ppDataSize); if (result != 0) { Debug.WriteLine("Failed to get raw input pre-parsed data size" + result + Marshal.GetLastWin32Error()); @@ -108,7 +109,7 @@ } IntPtr ppData = Marshal.AllocHGlobal((int)ppDataSize); - result = Win32.Function.GetRawInputDeviceInfo(hDevice, Win32.Const.RIDI_PREPARSEDDATA, ppData, ref ppDataSize); + result = Win32.Function.GetRawInputDeviceInfo(hDevice, RawInputDeviceInfoType.RIDI_PREPARSEDDATA, ppData, ref ppDataSize); if (result <= 0) { Debug.WriteLine("Failed to get raw input pre-parsed data" + result + Marshal.GetLastWin32Error()); @@ -125,7 +126,7 @@ public static string GetDeviceName(IntPtr device) { uint deviceNameSize = 256; - int result = Win32.Function.GetRawInputDeviceInfo(device, Win32.Const.RIDI_DEVICENAME, IntPtr.Zero, ref deviceNameSize); + int result = Win32.Function.GetRawInputDeviceInfo(device, RawInputDeviceInfoType.RIDI_DEVICENAME, IntPtr.Zero, ref deviceNameSize); if (result != 0) { return string.Empty; @@ -134,7 +135,7 @@ IntPtr deviceName = Marshal.AllocHGlobal((int)deviceNameSize * 2); // size is the character count not byte count try { - result = Win32.Function.GetRawInputDeviceInfo(device, Win32.Const.RIDI_DEVICENAME, deviceName, ref deviceNameSize); + result = Win32.Function.GetRawInputDeviceInfo(device, RawInputDeviceInfoType.RIDI_DEVICENAME, deviceName, ref deviceNameSize); if (result > 0) { return Marshal.PtrToStringAnsi(deviceName, result - 1); // -1 for NULL termination @@ -149,5 +150,41 @@ } + /// + /// + /// + /// + public static void PopulateDeviceList(TreeView aTreeView) + { + + //Get our list of devices + RAWINPUTDEVICELIST[] ridList = null; + uint deviceCount = 0; + int res = Win32.Function.GetRawInputDeviceList(ridList, ref deviceCount,(uint)Marshal.SizeOf(typeof(RAWINPUTDEVICELIST))); + if (res == -1) + { + //Just give up then + return; + } + + ridList = new RAWINPUTDEVICELIST[deviceCount]; + res = Win32.Function.GetRawInputDeviceList(ridList, ref deviceCount, (uint)Marshal.SizeOf(typeof(RAWINPUTDEVICELIST))); + if (res != deviceCount) + { + //Just give up then + return; + } + + //For each our device add an node to our treeview + foreach (RAWINPUTDEVICELIST device in ridList) + { + string name=GetDeviceName(device.hDevice); + aTreeView.Nodes.Add(name, name); + } + + //TreeNode node = null; + + } + } } \ No newline at end of file diff -r 89dfad9633b2 -r 687cace560d2 RemoteControlSample.csproj --- a/RemoteControlSample.csproj Sun Feb 15 13:30:54 2015 +0100 +++ b/RemoteControlSample.csproj Sun Feb 15 18:15:41 2015 +0100 @@ -131,6 +131,9 @@ Form + + MainForm.cs + Code diff -r 89dfad9633b2 -r 687cace560d2 Win32RawInput.cs --- a/Win32RawInput.cs Sun Feb 15 13:30:54 2015 +0100 +++ b/Win32RawInput.cs Sun Feb 15 18:15:41 2015 +0100 @@ -13,7 +13,14 @@ public extern static uint GetRawInputData(IntPtr hRawInput, uint uiCommand, IntPtr pData, ref uint pcbSize, uint cbSizeHeader); [DllImport("User32.dll", SetLastError=true)] - public extern static int GetRawInputDeviceInfo(IntPtr hDevice, uint uiCommand, IntPtr pData, ref uint pcbSize); + public extern static int GetRawInputDeviceInfo(IntPtr hDevice, RawInputDeviceInfoType uiCommand, IntPtr pData, ref uint pcbSize); + + [DllImport("user32.dll", SetLastError = true)] + public static extern int GetRawInputDeviceList( + [In, Out] RAWINPUTDEVICELIST[] InputdeviceList, + [In, Out] ref uint puiNumDevices, + [In] uint cbSize); + } @@ -64,33 +71,8 @@ public const int WM_KEYDOWN = 0x0100; public const int WM_INPUT = 0x00FF; - /// - /// GetRawInputDeviceInfo pData points to a string that contains the device name. - /// - public const uint RIDI_DEVICENAME = 0x20000007; - /// - /// GetRawInputDeviceInfo For this uiCommand only, the value in pcbSize is the character count (not the byte count). - /// - public const uint RIDI_DEVICEINFO = 0x2000000b; - /// - /// GetRawInputDeviceInfo pData points to an RID_DEVICE_INFO structure. - /// - public const uint RIDI_PREPARSEDDATA = 0x20000005; - - /// - /// Data comes from a mouse. - /// - public const uint RIM_TYPEMOUSE = 0; - /// - /// Data comes from a keyboard. - /// - public const uint RIM_TYPEKEYBOARD = 1; - /// - /// Data comes from an HID that is not a keyboard or a mouse. - /// - public const uint RIM_TYPEHID = 2; - + // public const int RID_INPUT = 0x10000003; public const int RID_HEADER = 0x10000005; @@ -181,12 +163,52 @@ public const int FAPPCOMMAND_OEM = 0x1000; } + /// + /// Introduced this enum for consistency and easy of use. + /// Naming of the Win32 constants were preserved. + /// + public enum RawInputDeviceType : uint + { + /// + /// Data comes from a mouse. + /// + RIM_TYPEMOUSE = 0, + /// + /// Data comes from a keyboard. + /// + RIM_TYPEKEYBOARD = 1, + /// + /// Data comes from an HID that is not a keyboard or a mouse. + /// + RIM_TYPEHID = 2 + } + + /// + /// Introduced this enum for consistency and easy of use. + /// Naming of the Win32 constants were preserved. + /// + public enum RawInputDeviceInfoType : uint + { + /// + /// GetRawInputDeviceInfo pData points to a string that contains the device name. + /// + RIDI_DEVICENAME = 0x20000007, + /// + /// GetRawInputDeviceInfo For this uiCommand only, the value in pcbSize is the character count (not the byte count). + /// + RIDI_DEVICEINFO = 0x2000000b, + /// + /// GetRawInputDeviceInfo pData points to an RID_DEVICE_INFO structure. + /// + RIDI_PREPARSEDDATA = 0x20000005 + } + [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct RAWINPUTDEVICELIST { public IntPtr hDevice; - public uint dwType; + public RawInputDeviceType dwType; } [StructLayout(LayoutKind.Sequential, Pack = 1)] @@ -206,7 +228,7 @@ public struct RAWINPUTHEADER { [MarshalAs(UnmanagedType.U4)] - public int dwType; + public RawInputDeviceType dwType; [MarshalAs(UnmanagedType.U4)] public int dwSize; public IntPtr hDevice; @@ -331,7 +353,8 @@ [FieldOffset(0)] public uint cbSize; [FieldOffset(4)] - public uint dwType; + [MarshalAsAttribute(UnmanagedType.U4)] + public RawInputDeviceType dwType; [FieldOffset(8)] public RID_DEVICE_INFO_MOUSE mouse; [FieldOffset(8)]