using Windows.Devices.Enumeration; using Windows.Devices.HumanInterfaceDevice; using Windows.Storage; using Windows.Storage.Streams; using System.Runtime.InteropServices.WindowsRuntime; using System.Text; namespace HIDdeviceTest { public partial class Form1 : Form { public Form1() { InitializeComponent(); EnumerateHidDevices(); } private void Form1_Load(object sender, EventArgs e) { } // Find HID devices. private async void EnumerateHidDevices() { // Microsoft Input Configuration Device. ushort vendorId = 0x046D; ushort productId = 0xC24A; ushort usagePage = 0xFF80; ushort usageId = 0x0080; // Create the selector. string selector = HidDevice.GetDeviceSelector(usagePage, usageId, vendorId, productId); // Enumerate devices using the selector. var devices = await DeviceInformation.FindAllAsync(selector); if (devices.Any()) { // At this point the device is available to communicate with // So we can send/receive HID reports from it or // query it for control descriptions. System.Diagnostics.Debug.WriteLine("HID devices found: " + devices.Count); // Open the target HID device. HidDevice device = await HidDevice.FromIdAsync(devices.ElementAt(0).Id, FileAccessMode.Read); if (device != null) { System.Diagnostics.Debug.WriteLine("Device not null, adding async calls"); // Input reports contain data from the device. device.InputReportReceived += async (sender, args) => { HidInputReport inputReport = args.Report; IBuffer buffer = inputReport.Data; // Create a DispatchedHandler as we are interracting with the UI directly and the // thread that this function is running on might not be the UI thread; // if a non-UI thread modifies the UI, an exception is thrown. StringBuilder data = new StringBuilder(); foreach (var d in buffer.ToArray()) { data.Append(d.ToString()); } System.Diagnostics.Debug.WriteLine("HID Input Report: " + inputReport.ToString() + "\nTotal number of bytes received: " + buffer.Length.ToString() + "\n Data: 0x" + data.ToString()); }; System.Diagnostics.Debug.WriteLine("Added input report received"); } else { System.Diagnostics.Debug.WriteLine("NO DEVICES TO LOOK AT"); } } else { // There were no HID devices that met the selector criteria. Console.Error.WriteLine("HID device not found"); } } } }