如何通过 USB HID API 从游戏控制器请求输入数据?

问题描述 投票:0回答:2

我一直在使用下面的 API 从我的游戏控制器获取数据,在我的新控制器到达之前它工作得很好,它具有超过 API 支持的 32 个按钮(Windows 游戏控制器控制面板小程序甚至不显示他们全部!)

Public Declare Function joyGetPosEx Lib "winmm.dll" (ByVal uJoyID As Long, pji As JOYINFOEX) As Long

我有 API,可以向任何给定供应商/产品 ID 的 USB 设备发送最多 65 个字节
我只需要知道要发送哪些字节。我找不到任何像这样的低级文档
我不能使用 directinput,因为这不是游戏,必须在后台运行,仍然允许使用控制器来玩游戏,而且我不想要这么大的不必要的依赖性

Public Function ReadAndWriteToDevice(MyVendorID As Long, MyProductID As Long, Optional Byte1 As Byte, Optional Byte2 As Byte, Optional Byte3 As Byte, Optional Byte4 As Byte, Optional Byte5 As Byte, Optional Byte6 As Byte, Optional Byte7 As Byte, Optional Byte8 As Byte, Optional ReadBytes As Long = 8, Optional Timeout_in_msec As Integer = 5000) As Byte()
    Dim i As Long
    Dim OutputReportData(65) As Byte
    Dim InputReportData(65) As Byte
    Dim RET() As Byte
    
    OutputReportData(1) = Byte1 ' the code is set up to only send 8 bytes so far but changing it to a max of 65 looks easy
    OutputReportData(2) = Byte2
    OutputReportData(3) = Byte3
    OutputReportData(4) = Byte4
    OutputReportData(5) = Byte5
    OutputReportData(6) = Byte6
    OutputReportData(7) = Byte7
    OutputReportData(8) = Byte8
    
    If MyDeviceDetected = False Then
        MyDeviceDetected = FindTheHid(MyVendorID, MyProductID)
    End If
    
    If MyDeviceDetected Then
        Call WriteToHID(OutputReportData)
        If ReadFromHID(InputReportData, , Timeout_in_msec, False) = -1 Then Exit Function
    End If
    
    If ReadBytes > 64 Or ReadBytes < 1 Then
        ReadAndWriteToDevice = InputReportData
    Else
        ReDim RET(1 To ReadBytes)
        For i = 1 To ReadBytes
            RET(i) = InputReportData(i)
        Next i
        ReadAndWriteToDevice = RET 'InputReportData(1)
    End If
End Function
vb6 gamepad gamepad-api
2个回答
2
投票

游戏控制器控制面板使用 DirectInput,仅限 32 个按钮。这是哪个游戏手柄?游戏手柄有这么多按钮并不常见。

如何从游戏控制器请求输入数据

您无需提出要求。控制器决定何时发送输入报告。对于大多数控制器,输入报告会定期发送,但某些控制器仅在输入更改时发送输入报告。

平台 API 缓冲输入报告,以便您的应用程序可以在方便时读取它们。

在 Windows 上,一旦您拥有 HID 设备的文件句柄,您就可以使用 ReadFile 读取输入报告。

我只需要知道要发送哪些字节。

如果您只读取输入,通常不需要编写任何内容。输出报告用于主机控制设备状态的隆隆声或 LED 等功能。

设备报告的大小和布局在设备的 HID 报告描述符中定义。如果您可以获得报告描述符(Windows 使这变得很困难),那么它应该会给您一些关于设备期望哪种数据的提示。


0
投票

我正在用 vb6 编码,到 xplane,使用许多 arduino。 现在我购买了 GoFlight MCPPRO,我想发送数据来打开一些 LED 灯并在 7 段显示屏上写入数字。

我有一个从 C 语言翻译而来的代码,但没有任何效果。我发送但什么也没做。 请帮我。 目标:3 要显示的消息:888 我要发送的代码:

公共函数 WriteToHID(ByRef DataToSend() As Byte,可选数据长度 As Long = 64) As Long

Dim numberOfBytesWritten As Long
Dim ByteValue As String
Dim result As Long

'The first byte is the Report ID
DataToSend(0) = 0

numberOfBytesWritten = 8

    ' Converte a mensagem em um array de bytes
result = WriteFile(HID_Write_Handle, DataToSend(0), Datalength + 1, numberOfBytesWritten, 0)

WriteToHID = numberOfBytesWritten - 1

结束功能

Dim OutputReportData(65) 作为字节 Dim InputReportData(65) As Byte

lstResults.Clear

lstResults.AddItem "***** HID Test Report *****"

OutputReportData(1) = &H3   '10 + adder
OutputReportData(2) = &H7F ' + adder
OutputReportData(3) = &H7F ' + 
OutputReportData(4) = &H7F 
OutputReportData(5) = &H0 
OutputReportData(6) = &H0 
OutputReportData(7) = &H0 
OutputReportData(8) = &H0

If MyDeviceDetected = False Then MyDeviceDetected = FindTheHid

If MyDeviceDetected = True Then
    'Me.Caption = OutputReportData
    Call WriteToHID(OutputReportData)
    Call ReadFromHID(InputReportData)
    
End If
© www.soinside.com 2019 - 2024. All rights reserved.