第二篇文章關於支持多個設備:

馮海亮:WindosRawInputPluginForUE4(2)?

zhuanlan.zhihu.com
圖標

github:

Lynnvon/RawInputPluginForUE4?

github.com
圖標

最近在做一個模擬飛行的項目時,需要外接飛行搖桿,我使用了官方的Windows Rawinput Plugin,但在使用的時候報了一個HIDStatusBufferTooSmall的warning,網上並沒有查到任何信息,但最終我還是找到了解決的辦法。

  1. 此插件只支持同時按下兩個按鍵,當大於2個的時候就會報HIDStatusBufferTooSmall的錯誤,我們找到插件的源代碼,在RawInputWindows.cpp的ParseInputData方法內修改一下提高同時按壓鍵數就可以了,我直接給賦值了40,就是說同時可以按壓40個鍵,當然你也可以修改為其他的數值。

if (HIDStatus != HIDP_STATUS_SUCCESS)
{
UE_LOG(LogRawInputWindows, Warning, TEXT("Failed to read button caps: %x:%s"), (int32)HIDStatus, *GetErrorString(HIDStatus));
}
else
{
//const int32 NumberOfButtons = ButtonCapsBuffer[0].Range.UsageMax - ButtonCapsBuffer[0].Range.UsageMin + 1;
/*當搖桿同時按下多個鍵時,上邊的會出錯,報HIDStatusBufferTooSmall錯誤,所以直接賦值*/
const int32 NumberOfButtons = 40;
const uint32 ButtonDataBufferSize = NumberOfButtons * sizeof(uint16);
uint16* ButtonDataBuffer = (uint16*)FMemory_Alloca(ButtonDataBufferSize);
FMemory::Memzero(ButtonDataBuffer, ButtonDataBufferSize);

uint32 UsageNumButtonCaps = NumberOfButtons;

HIDStatus = DLLPointers.HidP_GetUsages(HIDP_REPORT_TYPE::HidP_Input, ButtonCapsBuffer[0].UsagePage, 0, ButtonDataBuffer, &UsageNumButtonCaps, InPreParsedData, (PCHAR)InRawInputDataBuffer->data.hid.bRawData, InRawInputDataBuffer->data.hid.dwSizeHid);
if (HIDStatus != HIDP_STATUS_SUCCESS)
{
UE_LOG(LogRawInputWindows, Warning, TEXT("Failed to read button data: %x:%s"), (int32)HIDStatus, *GetErrorString(HIDStatus));

2.插件修改了源碼後需要重新編譯,我的ue4是launcher下載的,我們需要將該插件內Intermediate和Binaries文件夾刪除,再打開項目就會提示缺少rawinput.dll,然後點擊確定重新編譯。

3.當插件在ue4的插件目錄時,無論如何都不能編譯成功,最終我將該插件放到了項目的Plugins目錄中,編譯成功。

4.還有一種情況,就是默認該插件只有8個axis和20個button,我需要更多地button和axis,需要把源碼內的button和axis增加至你需要的個數,然後重新編譯,這個比較簡單,就不一一說了,只需要小心一些,修改好幾個文件呢。特別的,就是RawInputWindows.h中修改兩個宏定義。

#include "hidsdi.h"

#define MAX_NUM_CONTROLLER_BUTTONS 40 //修改為你需要的數目 最大button數目
#define MAX_NUM_CONTROLLER_ANALOG 8 //最大axis數目
#define RAW_INPUT_ERROR (uint32)(-1)

其他的基本都是這樣的:

const FGamepadKeyNames::Type FRawInputKeyNames::GenericUSBController_Button19("GenericUSBController_Button19");
const FGamepadKeyNames::Type FRawInputKeyNames::GenericUSBController_Button20("GenericUSBController_Button20");
//上邊兩個是原有的,下邊的是添加的 代碼一樣,只需要該id就行
const FGamepadKeyNames::Type FRawInputKeyNames::GenericUSBController_Button21("GenericUSBController_Button21");
const FGamepadKeyNames::Type FRawInputKeyNames::GenericUSBController_Button22("GenericUSBController_Button22");
const FGamepadKeyNames::Type FRawInputKeyNames::GenericUSBController_Button23("GenericUSBController_Button23");
const FGamepadKeyNames::Type FRawInputKeyNames::GenericUSBController_Button24("GenericUSBController_Button24");

5.在ue4中註冊外接設備時,需要在ue4中設置以下內容

在VendorID中填寫外設的VID,在ProductID填寫PID,外設的vid和pid在設備管理器中查看。

以上邊的為例,你需要在ue4中填寫0x1038和0x1392。

以上就是所有有關WindowsRawinput的內容。

推薦閱讀:

相关文章