HidDevice 類別

定義

代表最上層集合和對應的裝置。

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
繼承
Object Platform::Object IInspectable HidDevice
屬性
實作

Windows 需求

裝置系列
Windows 10 (已於 10.0.10240.0 - for Xbox, see UWP features that aren't yet supported on Xbox 引進)
API contract
Windows.Foundation.UniversalApiContract (已於 v1.0 引進)

範例

如需示範如何使用這個類別的完整範例,請參閱 自訂 HID 裝置範例

下列範例示範使用 XAML 和 C# 建置的 UWP 應用程式如何使用 GetDeviceSelector 方法來建立特定 HID 裝置的選取器 (,在此案例中為特定 HID 裝置建立選取器,Microsoft 輸入設定裝置) ,然後使用 FromIdAsync 方法來開啟該裝置的連線。

注意

此程式碼片段會嘗試尋找系統上可能不存在的 HID 裝置。 若要在您的系統上成功測試程式碼,您應該以有效值更新 vendorId、productId、usagePage、usageId。

  1. 開啟裝置管理員
  2. 展開 人類介面裝置
  3. 在此範例中找出 HID 裝置 (,我們選擇 [Microsoft 輸入設定裝置 ])
  4. 以滑鼠右鍵按一下裝置,然後選取 [屬性]
  5. 在 [屬性] 中,選取 [ 詳細資料] 索引標籤
  6. 在 [詳細資料] 索引標籤上,從 [屬性] 下拉式清單中選取[硬體識別碼]
  7. HID 詳細資料會顯示在 [ ] 方塊中:
裝置管理員的 HID 詳細資料
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";
            }
        }
    }
}

備註

如需使用此類別的詳細資訊,包括限制,請參閱 支援人類介面裝置 (HID) 自訂 HID 裝置範例

使用此類別來存取 HID 裝置的應用程式必須在資訊清單的[功能] 節點中包含特定的DeviceCapability資料。 此資料會識別裝置及其用途 (或函式) 。 如需詳細資訊,請參閱 如何指定 HID 的裝置功能

屬性

ProductId

取得指定 HID 裝置的產品識別碼。

UsageId

取得指定 HID 裝置的使用識別碼。

UsagePage

取得最上層集合的使用頁面。

VendorId

取得指定 HID 裝置的廠商識別碼。

Version

取得指定 HID 裝置的版本或修訂編號。

方法

Close()

關閉主機與指定 HID 裝置之間的連線。

CreateFeatureReport()

建立主機將傳送至裝置的唯一或預設功能報告。

CreateFeatureReport(UInt16)

建立 由 reportId 參數識別的功能報表,主機會傳送至裝置。

CreateOutputReport()

建立主機將傳送至裝置的唯一或預設輸出報告。

CreateOutputReport(UInt16)

建立由 reportId 參數識別的輸出報表,主機會將該報表傳送至裝置。

Dispose()

執行與釋放 (Free)、釋放 (Release) 或重設 Unmanaged 資源相關聯之應用程式定義的工作。

FromIdAsync(String, FileAccessMode)

開啟 deviceId 參數所識別之裝置的控制碼。 存取類型是由 accessMode 參數所指定。

GetBooleanControlDescriptions(HidReportType, UInt16, UInt16)

擷取指定 HID 裝置之布林控制項的描述。

GetDeviceSelector(UInt16, UInt16)

根據指定的 usagePageusageId,擷取 AQS) 字串 (進階查詢語法。

GetDeviceSelector(UInt16, UInt16, UInt16, UInt16)

根據指定的usagePage、usageIdvendorIdproductId,擷取 AQS) 字串 (進階查詢語法。

GetFeatureReportAsync()

以非同步方式從指定的 HID 裝置擷取第一個或預設的功能報告。

GetFeatureReportAsync(UInt16)

針對指定的 HID 裝置,以非同步方式擷取 reportId 參數所識別的功能報表。

GetInputReportAsync()

以非同步方式從指定的 HID 裝置擷取預設值或第一個輸入報告。

GetInputReportAsync(UInt16)

從指定的 HID 裝置,以非同步方式擷取 reportId 參數所識別的輸入報表。

GetNumericControlDescriptions(HidReportType, UInt16, UInt16)

擷取指定 HID 裝置之數值控制項的描述。

SendFeatureReportAsync(HidFeatureReport)

以非同步方式將功能報告從主機傳送至裝置。

SendOutputReportAsync(HidOutputReport)

以非同步方式將輸出報告從主機傳送至裝置。

事件

InputReportReceived

呼叫 GetInputReportAsync () GetInputReportAsync (System.UInt16 reportId) 時,建立事件接聽程式來處理裝置發出的輸入報告。

適用於

另請參閱