HidDevice Class

Definition

Represents a top-level collection and the corresponding device.

public ref class HidDevice sealed : IClosable
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
class HidDevice final : IClosable
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
public sealed class HidDevice : System.IDisposable
Public NotInheritable Class HidDevice
Implements IDisposable
Inheritance
Object Platform::Object IInspectable HidDevice
Attributes
Implements

Windows requirements

Device family
Windows 10 (introduced in 10.0.10240.0 - for Xbox, see UWP features that aren't yet supported on Xbox)
API contract
Windows.Foundation.UniversalApiContract (introduced in v1.0)

Examples

For a complete sample that demonstrates how to use this class, see Custom HID device sample.

The following example demonstrates how a UWP app built with XAML and C# uses the GetDeviceSelector method to create a selector for a specific HID device (in this case, Microsoft Input Configuration Device) and then uses FromIdAsync method to open a connection to that device.

Note

This snippet attempts to find a HID device that might not be present on your system. To successfully test the code on your system, you should update vendorId, productId, usagePage, usageId with valid values.

  1. Open Device Manager
  2. Expand Human Interface Devices
  3. Locate a HID device (for this example we chose Microsoft Input Configuration Device)
  4. Right click the device and select Properties
  5. In Properties, select the Details tab
  6. On the Details tab, select Hardware Ids from the Property drop down
  7. The HID details are displayed in the Value box:
HID details from Device Manager
using System;
using System.Linq;
using Windows.Devices.Enumeration;
using Windows.Devices.HumanInterfaceDevice;
using Windows.Storage;
using Windows.UI.Xaml.Controls;

namespace HIDdeviceTest
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            EnumerateHidDevices();
        }

        // Find HID devices.
        private async void EnumerateHidDevices()
        {
            // Microsoft Input Configuration Device.
            ushort vendorId = 0x045E;
            ushort productId = 0x07CD;
            ushort usagePage = 0x000D;
            ushort usageId = 0x000E;

            // 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.
                info.Text = "HID devices found: " + devices.Count;

                // Open the target HID device.
                HidDevice device = 
                    await HidDevice.FromIdAsync(devices.ElementAt(0).Id,
                    FileAccessMode.ReadWrite);

                if (device != null)
                {
                    // 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.

                        await this.Dispatcher.RunAsync(
                            CoreDispatcherPriority.Normal,
                            new DispatchedHandler(() =>
                            {
                                info.Text += "\nHID Input Report: " + inputReport.ToString() + 
                                "\nTotal number of bytes received: " + buffer.Length.ToString();
                            }));
                    };
                }

            }
            else
            {
                // There were no HID devices that met the selector criteria.
                info.Text = "HID device not found";
            }
        }
    }
}

Remarks

For more information about using this class, including limitations, see Supporting human interface devices (HID) and Custom HID device sample.

Apps that use this class to access a HID device must include specific DeviceCapability data in the Capabilities node of its manifest. This data identifies the device and its purpose (or function). For more information, see How to specify device capabilities for HID.

Properties

ProductId

Gets the product identifier for the given HID device.

UsageId

Gets the usage identifier for the given HID device.

UsagePage

Gets the usage page of the top-level collection.

VendorId

Gets the vendor identifier for the given HID device.

Version

Gets the version, or revision, number for the given HID device.

Methods

Close()

Closes the connection between the host and the given HID device.

CreateFeatureReport()

Creates the only, or default, feature report that the host will send to the device.

CreateFeatureReport(UInt16)

Creates a feature report, identified by the reportId parameter, that the host will send to the device.

CreateOutputReport()

Creates the only, or default, output report that the host will send to the device.

CreateOutputReport(UInt16)

Creates an output report, identified by the reportId parameter, that the host will send to the device.

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

FromIdAsync(String, FileAccessMode)

Opens a handle to the device identified by the deviceId parameter. The access type is specified by the accessMode parameter.

GetBooleanControlDescriptions(HidReportType, UInt16, UInt16)

Retrieves the descriptions of the boolean controls for the given HID device.

GetDeviceSelector(UInt16, UInt16)

Retrieves an Advanced Query Syntax (AQS) string based on the given usagePage and usageId.

GetDeviceSelector(UInt16, UInt16, UInt16, UInt16)

Retrieves an Advanced Query Syntax (AQS) string based on the given usagePage, usageId, vendorId, and productId.

GetFeatureReportAsync()

Asynchronously retrieves the first, or default, feature report from the given HID device.

GetFeatureReportAsync(UInt16)

Asynchronously retrieves a feature report, identified by the reportId parameter, for the given HID device.

GetInputReportAsync()

Asynchronously retrieves the default, or first, input report from the given HID device.

GetInputReportAsync(UInt16)

Asynchronously retrieves an input report, identified by the reportId parameter, from the given HID device.

GetNumericControlDescriptions(HidReportType, UInt16, UInt16)

Retrieves the descriptions of the numeric controls for the given HID device.

SendFeatureReportAsync(HidFeatureReport)

Sends an feature report asynchronously from the host to the device.

SendOutputReportAsync(HidOutputReport)

Sends an output report asynchronously from the host to the device.

Events

InputReportReceived

Establishes an event listener to handle input reports issued by the device when either GetInputReportAsync() or GetInputReportAsync(System.UInt16 reportId) is called.

Applies to

See also