HidDevice HidDevice HidDevice HidDevice HidDevice Class

Definition

Represents a top-level collection and the corresponding device.

public : sealed class HidDevice : IHidDevice, IClosable
struct winrt::Windows::Devices::HumanInterfaceDevice::HidDevice : IHidDevice, IClosable
public sealed class HidDevice : IHidDevice, IDisposable
Public NotInheritable Class HidDevice Implements IHidDevice, IDisposable
// This class does not provide a public constructor.
Attributes
Windows 10 requirements
Device family
Windows 10 (introduced v10.0.10240.0 - for Xbox, see UWP features that aren't yet supported on Xbox)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)

Examples

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";
            }
        }
    }
}

Properties

ProductId ProductId ProductId ProductId ProductId

Gets the product identifier for the given HID device.

UsageId UsageId UsageId UsageId UsageId

Gets the usage identifier for the given HID device.

UsagePage UsagePage UsagePage UsagePage UsagePage

Gets the usage page of the top-level collection.

VendorId VendorId VendorId VendorId VendorId

Gets the vendor identifier for the given HID device.

Version Version Version Version Version

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

Methods

Close() Close() Close() Close() Close()

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

CreateFeatureReport() CreateFeatureReport() CreateFeatureReport() CreateFeatureReport() CreateFeatureReport()

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

CreateFeatureReport(UInt16) CreateFeatureReport(UInt16) CreateFeatureReport(UInt16) CreateFeatureReport(UInt16) CreateFeatureReport(UInt16)

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

CreateOutputReport() CreateOutputReport() CreateOutputReport() CreateOutputReport() CreateOutputReport()

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

CreateOutputReport(UInt16) CreateOutputReport(UInt16) CreateOutputReport(UInt16) CreateOutputReport(UInt16) CreateOutputReport(UInt16)

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

Dispose() Dispose() Dispose() Dispose() Dispose()

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

FromIdAsync(String, FileAccessMode) FromIdAsync(String, FileAccessMode) FromIdAsync(String, FileAccessMode) FromIdAsync(String, FileAccessMode) 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) GetBooleanControlDescriptions(HidReportType, UInt16, UInt16) GetBooleanControlDescriptions(HidReportType, UInt16, UInt16) GetBooleanControlDescriptions(HidReportType, UInt16, UInt16) GetBooleanControlDescriptions(HidReportType, UInt16, UInt16)

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

GetDeviceSelector(UInt16, UInt16) GetDeviceSelector(UInt16, UInt16) GetDeviceSelector(UInt16, UInt16) GetDeviceSelector(UInt16, UInt16) GetDeviceSelector(UInt16, UInt16)

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

GetDeviceSelector(UInt16, UInt16, UInt16, UInt16) GetDeviceSelector(UInt16, UInt16, UInt16, UInt16) GetDeviceSelector(UInt16, UInt16, UInt16, UInt16) GetDeviceSelector(UInt16, UInt16, UInt16, UInt16) GetDeviceSelector(UInt16, UInt16, UInt16, UInt16)

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

GetFeatureReportAsync() GetFeatureReportAsync() GetFeatureReportAsync() GetFeatureReportAsync() GetFeatureReportAsync()

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

GetFeatureReportAsync(UInt16) GetFeatureReportAsync(UInt16) GetFeatureReportAsync(UInt16) GetFeatureReportAsync(UInt16) GetFeatureReportAsync(UInt16)

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

GetInputReportAsync() GetInputReportAsync() GetInputReportAsync() GetInputReportAsync() GetInputReportAsync()

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

GetInputReportAsync(UInt16) GetInputReportAsync(UInt16) GetInputReportAsync(UInt16) GetInputReportAsync(UInt16) GetInputReportAsync(UInt16)

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

GetNumericControlDescriptions(HidReportType, UInt16, UInt16) GetNumericControlDescriptions(HidReportType, UInt16, UInt16) GetNumericControlDescriptions(HidReportType, UInt16, UInt16) GetNumericControlDescriptions(HidReportType, UInt16, UInt16) GetNumericControlDescriptions(HidReportType, UInt16, UInt16)

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

SendFeatureReportAsync(HidFeatureReport) SendFeatureReportAsync(HidFeatureReport) SendFeatureReportAsync(HidFeatureReport) SendFeatureReportAsync(HidFeatureReport) SendFeatureReportAsync(HidFeatureReport)

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

SendOutputReportAsync(HidOutputReport) SendOutputReportAsync(HidOutputReport) SendOutputReportAsync(HidOutputReport) SendOutputReportAsync(HidOutputReport) SendOutputReportAsync(HidOutputReport)

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

Events

InputReportReceived InputReportReceived InputReportReceived InputReportReceived InputReportReceived

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

See Also

  • # Reference T:Windows.Foundation.IClosable ### Samples [Custom HID device sample](https://github.com/Microsoft/Windows-universal-samples/tree/6370138b150ca8a34ff86de376ab6408c5587f5d/Samples/CustomHidDeviceAccess)