共用方式為


XInput 和 DirectInput 功能的比較

重要

如需電腦和 Xbox 上透過 Microsoft Game Development Kit (GDK) 支援下一代輸入 API 的詳細資訊,請參閱 GameInput API

本檔比較控制器輸入的 XInput 和 DirectInput 實作,以及如何同時支援 XInput 裝置和舊版 DirectInput 裝置。

Windows 市集應用程式不支援 DirectInput

概觀

XInput 可讓應用程式接收來自 XUSB 控制器的輸入。 API 可透過 DirectX SDK 取得,而驅動程式可透過 Windows Update 取得。

透過 DirectInput 使用 XInput 有幾個優點:

  • XInput 較容易使用,而且需要比 DirectInput 更少的設定
  • Xbox 和 Windows 程式設計都會使用相同的核心 API 集合,讓程式設計更容易轉譯跨平臺
  • 將會有大型安裝的控制器基底
  • 只有在使用 XInput API 時,XInput 裝置才會有震動功能

搭配 DirectInput 使用 XUSB 控制器

XUSB 控制器會在 DirectInput 上 正確列舉,而且可與 DirectInputAPIs 搭配使用。 不過,DirectInput 實作中將會遺漏 XInput 所提供的某些功能:

  • 左右觸發程式按鈕將做為單一按鈕,而不是獨立
  • 無法取得震動效果
  • 無法查詢頭戴式裝置

DirectInput 中的 左右觸發程式組合是設計方式。 遊戲一律假設 DirectInput 裝置軸會在沒有使用者與裝置互動時置中。 不過,當觸發程式未保留時,較新的控制器是設計來註冊最小值,而不是置中。 因此,較舊的遊戲會假設使用者互動。

解決方案是結合觸發程式,將一個觸發程式設定為正方向,另一個觸發程式設定為負方向,因此沒有任何使用者互動表示 位於中心「控制項」的 DirectInput

若要個別測試觸發程式值,您必須使用 XInput。

XInput 和 DirectInput 並排

僅支援 XInput,您的遊戲將無法與舊版 DirectInput 裝置搭配使用。 XInput 無法辨識這些裝置。

如果您想要讓遊戲支援舊版 DirectInput 裝置,您可以並存使用 DirectInput 和 XInput。 列舉 DirectInput 裝置時,所有 DirectInput 裝置都會正確列舉。 所有 XInput 裝置都會顯示為 XInput 和 DirectInput 裝置,但不應該透過 DirectInput 處理它們。 您必須判斷哪些 DirectInput 裝置是舊版裝置,以及哪些是 XInput 裝置,並從 DirectInput 裝置的列舉中移除它們。

若要這樣做,請將此程式碼插入 DirectInput 列舉回呼中:

#include <wbemidl.h>
#include <oleauto.h>

#ifndef SAFE_RELEASE
#define SAFE_RELEASE(p) { if (p) { (p)->Release(); (p) = nullptr; } }
#endif

//-----------------------------------------------------------------------------
// Enum each PNP device using WMI and check each device ID to see if it contains 
// "IG_" (ex. "VID_0000&PID_0000&IG_00"). If it does, then it's an XInput device
// Unfortunately this information cannot be found by just using DirectInput 
//-----------------------------------------------------------------------------
BOOL IsXInputDevice( const GUID* pGuidProductFromDirectInput )
{
    IWbemLocator*           pIWbemLocator = nullptr;
    IEnumWbemClassObject*   pEnumDevices = nullptr;
    IWbemClassObject*       pDevices[20] = {};
    IWbemServices*          pIWbemServices = nullptr;
    BSTR                    bstrNamespace = nullptr;
    BSTR                    bstrDeviceID = nullptr;
    BSTR                    bstrClassName = nullptr;
    bool                    bIsXinputDevice = false;
    
    // CoInit if needed
    HRESULT hr = CoInitialize(nullptr);
    bool bCleanupCOM = SUCCEEDED(hr);

    // So we can call VariantClear() later, even if we never had a successful IWbemClassObject::Get().
    VARIANT var = {};
    VariantInit(&var);

    // Create WMI
    hr = CoCreateInstance(__uuidof(WbemLocator),
        nullptr,
        CLSCTX_INPROC_SERVER,
        __uuidof(IWbemLocator),
        (LPVOID*)&pIWbemLocator);
    if (FAILED(hr) || pIWbemLocator == nullptr)
        goto LCleanup;

    bstrNamespace = SysAllocString(L"\\\\.\\root\\cimv2");  if (bstrNamespace == nullptr) goto LCleanup;
    bstrClassName = SysAllocString(L"Win32_PNPEntity");     if (bstrClassName == nullptr) goto LCleanup;
    bstrDeviceID = SysAllocString(L"DeviceID");             if (bstrDeviceID == nullptr)  goto LCleanup;
    
    // Connect to WMI 
    hr = pIWbemLocator->ConnectServer(bstrNamespace, nullptr, nullptr, 0L,
        0L, nullptr, nullptr, &pIWbemServices);
    if (FAILED(hr) || pIWbemServices == nullptr)
        goto LCleanup;

    // Switch security level to IMPERSONATE. 
    hr = CoSetProxyBlanket(pIWbemServices,
        RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, nullptr,
        RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE,
        nullptr, EOAC_NONE);
    if ( FAILED(hr) )
        goto LCleanup;

    hr = pIWbemServices->CreateInstanceEnum(bstrClassName, 0, nullptr, &pEnumDevices);
    if (FAILED(hr) || pEnumDevices == nullptr)
        goto LCleanup;

    // Loop over all devices
    for (;;)
    {
        ULONG uReturned = 0;
        hr = pEnumDevices->Next(10000, _countof(pDevices), pDevices, &uReturned);
        if (FAILED(hr))
            goto LCleanup;
        if (uReturned == 0)
            break;

        for (size_t iDevice = 0; iDevice < uReturned; ++iDevice)
        {
            // For each device, get its device ID
            hr = pDevices[iDevice]->Get(bstrDeviceID, 0L, &var, nullptr, nullptr);
            if (SUCCEEDED(hr) && var.vt == VT_BSTR && var.bstrVal != nullptr)
            {
                // Check if the device ID contains "IG_".  If it does, then it's an XInput device
                // This information cannot be found from DirectInput 
                if (wcsstr(var.bstrVal, L"IG_"))
                {
                    // If it does, then get the VID/PID from var.bstrVal
                    DWORD dwPid = 0, dwVid = 0;
                    WCHAR* strVid = wcsstr(var.bstrVal, L"VID_");
                    if (strVid && swscanf_s(strVid, L"VID_%4X", &dwVid) != 1)
                        dwVid = 0;
                    WCHAR* strPid = wcsstr(var.bstrVal, L"PID_");
                    if (strPid && swscanf_s(strPid, L"PID_%4X", &dwPid) != 1)
                        dwPid = 0;

                    // Compare the VID/PID to the DInput device
                    DWORD dwVidPid = MAKELONG(dwVid, dwPid);
                    if (dwVidPid == pGuidProductFromDirectInput->Data1)
                    {
                        bIsXinputDevice = true;
                        goto LCleanup;
                    }
                }
            }
            VariantClear(&var);
            SAFE_RELEASE(pDevices[iDevice]);
        }
    }

LCleanup:
    VariantClear(&var);
    
    if(bstrNamespace)
        SysFreeString(bstrNamespace);
    if(bstrDeviceID)
        SysFreeString(bstrDeviceID);
    if(bstrClassName)
        SysFreeString(bstrClassName);
        
    for (size_t iDevice = 0; iDevice < _countof(pDevices); ++iDevice)
        SAFE_RELEASE(pDevices[iDevice]);

    SAFE_RELEASE(pEnumDevices);
    SAFE_RELEASE(pIWbemLocator);
    SAFE_RELEASE(pIWbemServices);

    if(bCleanupCOM)
        CoUninitialize();

    return bIsXinputDevice;
}


//-----------------------------------------------------------------------------
// Name: EnumJoysticksCallback()
// Desc: Called once for each enumerated joystick. If we find one, create a
//       device interface on it so we can play with it.
//-----------------------------------------------------------------------------
BOOL CALLBACK EnumJoysticksCallback( const DIDEVICEINSTANCE* pdidInstance,
                                     VOID* pContext )
{
    if( IsXInputDevice( &pdidInstance->guidProduct ) )
        return DIENUM_CONTINUE;

     // Device is verified not XInput, so add it to the list of DInput devices

     return DIENUM_CONTINUE;    
}

此程式碼的稍微改善版本位於舊版 DirectInput Joystick 範例中。

開始使用 XInput

程式設計參考