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

时间:2019-05-24 16:00:10

标签: c# .net dll hid

我正在研究一个项目,该项目使用微处理器模拟由HID键盘和HID鼠标组成的USB复合设备。我的设备可以正确枚举并与Windows 7 x64和Raspbian主机一起使用,并且一切正常,但是我遇到问题的地方是使Winforms应用程序(使用HidSharp)打开连接的复合设备,这样我就可以获取键盘端点中的原始数据。

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

/*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(根据文件属性)。

提前感谢您的任何建议!

2 个答案:

答案 0 :(得分:1)

因此我在HIDSharp论坛上四处询问,并得到了answer from the dev

结果是Windows不允许您将HID Keyboard设备作为安全性“功能”打开,因此HIDSharp将始终无法打开HID Keyboard设备的数据流。

答案 1 :(得分:-1)

是的...几天的搜索...并找到了您的帖子。我的老板,向导向我指出了一个相当优雅的(低级)解决方案。您必须注册Windows钩子并捕获击键。在确定是否是您的输入时,您必须具有创造力,但至少您可以在不集中精力的情况下捕获/吃掉击键...。

相关问题