为什么我无法使用 HIDSharp 连接到我的 USB 复合设备?

Why can't I connect to my USB Composite Device with HIDSharp?

我正在开展一个项目,该项目使用微处理器模拟由 HID 键盘和 HID 鼠标组成的 USB 复合设备。我的设备在我的 Windows 7 x64 和 Raspbian 主机上正确枚举和运行,一切看起来都不错,但我 运行 遇到问题的地方是获取我的 winforms 应用程序(使用 HidSharp) 打开连接的复合设备,以便我可以获取键盘端点中的原始数据。

问题似乎出在 TryOpen() 函数上,因为我可以通过匹配 VID 和 PID 找到连接的设备,我分配了设备信息和报告描述符,但是当我尝试通过 TryOpen 打开数据流时() 它失败了,我不知道为什么。不幸的是,该函数只有 returns 一个布尔值,所以我不知道它为什么会失败,只是它无法打开数据流。我想知道打开一个我不知道的复合设备是否有什么有趣的地方?我查找设备和打开数据流的代码如下:

/*These vars are part of the class*/
byte[] keyboardBuffer;  //EP1
HidSharp.Reports.Input.HidDeviceInputReceiver InputReceiver;
HidSharp.Reports.ReportDescriptor KeyboardRptDescriptor;
HidStream KeyboardStream;
HidDevice KeyboardDevice;

private void FindDevice()
{
    var list = DeviceList.Local;
    var stopwatch = Stopwatch.StartNew();
    var hidDeviceList = list.GetHidDevices().ToArray();

    foreach (HidDevice d in hidDeviceList)
    {

        if (d.VendorID == 0x0000 && d.ProductID == 0xA0A0)
        {
            /*Proper VID and PID Found*/
            if (d.GetProductName() == "Keyboard")
            {
                KeyboardDevice = d;
                KeyboardRptDescriptor = KeyboardDevice.GetReportDescriptor();

            }

        }

    }

    if (KeyboardDevice != null)
    {
        /*Device Found, open the datastream*/
        if (KeyboardDevice.TryOpen(out KeyboardStream))    //PROBLEM LINE - Always False?
        {
            KeyboardReport = KeyboardRptDescriptor.InputReports.FirstOrDefault();
            keyboardBuffer = new byte[KeyboardDevice.GetMaxInputReportLength()];
            InputParser = KeyboardReport.DeviceItem.CreateDeviceItemInputParser();
            InputReceiver = KeyboardRptDescriptor.CreateHidDeviceInputReceiver();
            InputReceiver.Received -= new EventHandler(HidInputReceived);
            InputReceiver.Received += new EventHandler(HidInputReceived);
            InputReceiver.Start(KeyboardStream);
        } else {
            rtb_hidLog.AppendText("Unable to connect to device\r\n");
        }


    }
    else
    {
        rtb_hidLog.AppendText("No Device Found\r\n");
    }

}

现在我只尝试从 HID 键盘读取,一旦我整理好键盘就会添加鼠标。找到设备似乎没有问题,但为什么打开它给我这样的问题?我的 HIDSharp 库似乎是 v2.0.2.0(根据文件属性)。

提前感谢您的任何建议!

所以我在 HIDSharp 论坛上询问了这件事,得到了一个 answer from the dev

事实证明 Windows 不允许您打开 HID 键盘设备作为安全措施 "feature",因此 HIDSharp 将始终无法打开 HID 键盘设备的数据流。

是的...搜索了几天...找到了您的 post。我的老板,向导,向我指出了一个相当优雅(低级别)的解决方案。您必须注册 windows 钩子并捕捉击键。您必须创造性地确定它是否是您的输入,但至少您可以 capture/eat 在没有焦点的情况下击键....