Working with the New Windows Runtime HID APIs in Windows 8.1

Introduction

As part of Windows 8.1, Microsoft will be introducing over 5000 new APIs enabling a bunch of new features under the umbrella of its flag ship product – the next version of the Windows operating system.

One of the new features to be introduced as part of Windows 8.1 is the programmatic access to Human Interface Devices through APIs.

The availability of these APIs enabled developers to build applications that can connect to a variety of devices (both wired and wireless) that support the HID (Human Interface Device) protocol (http://www.usb.org/developers/devclass_docs/HID1_11.pdf)

These APIs reside in the Windows.Devices.HumanInterfaceDevice namespace. There are two use cases for the HID APIs – (1) to enable the HID manufacturer to build an application to allow users to interact and control the device, and (2) to enable application developers to build applications that can work with the HID devices.

The APIs in the Windows.Devices.HumanInterfaceDevice namespace support most of the HID devices, except the top-level application collection represented by the following usage pages:

  • HID_USAGE_PAGE_UNDEFINED
  • HID_USAGE_PAGE_GENERIC
  • HID_USAGE_GENERIC_KEYBOARD
  • HID_USAGE_GENERIC_KEYPAD
  • HID_USAGE_GENERIC_SYSTEM_CTL
  • HID_USAGE_PAGE_KEYBOARD
  • HID_USAGE_PAGE_CONSUMER
  • HID_USAGE_PAGE_DIGITIZER
  • HID_USAGE_PAGE_SENSOR
  • HID_USAGE_PAGE_BARCODE_SCANNER
  • HID_USAGE_PAGE_WEIGHING_DEVICE
  • HID_USAGE_PAGE_MAGNETIC_STRIPE_READER
  • HID_USAGE_PAGE_TELEPHONY

Another limitation of the Windows 8.1 APIs is that only on-the-box device drivers are supported with these APIs. Vendor supplied device drivers are not supported through these APIs.

A reference to an HID device selector can be obtained programmatically by calling the HidDevce.GetDeviceSelector API and passing in vendor identifier, product identifier, usage identifier and usage page.  A list of devices can then be obtained by calling DeviceInformation.FillAllAsync API and passing in the device selector.

To get the HID device object, call the HidDevice.FromIdAsync API and pass in the device we got in the previous step.

Hands On

Create a new Windows Store application titled Windows8.1HIDDemo

New Windows Store Application
New Windows Store Application

In our application, we will list all the HID devices and Add a Button control and a ListBox Control.

On the Click event of the Button control, we will retrieve the HID devices.

In the code-behind for MainPage.xaml, include the Windows.Devices.HumanInterfaceDevice and Windows.Devices.Enumeration namespaces.

// MainPage.xaml.cs

using Windows.Devices.Enumeration;
using Windows.Devices.HumanInterfaceDevice;

Here is the code to retrieve the HID devices:

private async void buttonListHIDDevices_Click(object sender, RoutedEventArgs e)
        {
            string deviceSelector = HidDevice.GetDeviceSelector(0xFF00, 0x0001);
            var deviceInformationCollection = await DeviceInformation.FindAllAsync(deviceSelector);
            foreach (DeviceInformation d in deviceInformationCollection)
            {
                HidDevice hidDevice = await HidDevice.FromIdAsync(deviceInformationCollection.ElementAt(0).Id,
                                   FileAccessMode.Read);
                listBoxDeviceList.Items.Add(hidDevice.ProductID);
            }
 
        }

We will first call HidDevice.GetDeviceSelector and pass in the HID usage page and HID Usage ID of the device. For simplification, we have hard coded in our example to 0xFF00 and 0X0001. If you have access to an HID device, which has a different HID Usage ID, please change this to the correct values. Please note that the example will not work for you out of the box.

After we get the selector, we will call DeviceInformation.FindAllAsync API and pass in the selector. This will return the DeviceInformationCollection object. If there are HID devices on the system which that match the usage page and usage ID, they will be included in the collection.

Finally, we retrieve the HID device version and add it into our HID Device ListBox.

The above snippet is a simple example of how to use HID APIs in Windows 8.1.

You can retrieve data from a HIDDevice using the HidInputReport.GetNumericControl API.

One thing to note about applications that interact with HID devices is that they cannot save settings and hence they are per user apps.

Summary

In this article, we learned the basics about HID devices and the support for HID in Windows 8.1. I hope you have found this information useful.

About the author

Vipul Patel is a Program Manager currently working at Amazon Corporation. He has formerly worked at Microsoft in the Lync team and in the .NET team (in the Base Class libraries and the Debugging and Profiling team). He can be reached at vipul.patel@hotmail.com



Related Articles

Downloads

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • Hundreds of millions of users have adopted public cloud storage solutions to satisfy their Private Online File Sharing and Collaboration (OFS) needs. With new headlines on cloud privacy issues appearing almost daily, the need to explore private alternatives has never been stronger. Join ESG Senior Analyst Terri McClure and Connected Data in this on-demand webinar to take a look at the business drivers behind OFS adoption, how organizations can benefit from on-premise deployments, and emerging private OFS …

Most Popular Programming Stories

More for Developers

RSS Feeds