次の方法で共有


IGameInput::RegisterDeviceCallback

システムでデバイスが接続または切断されるたびに呼び出される GameInputDeviceCallback 関数を登録します。 デバイスのプロパティが変更されたときに関数を呼び出すこともできます。

構文

HRESULT RegisterDeviceCallback(  
         IGameInputDevice* device,  
         GameInputKind inputKind,  
         GameInputDeviceStatus statusFilter,  
         GameInputEnumerationKind enumerationKind,  
         void* context,  
         GameInputDeviceCallback callbackFunc,  
         GameInputCallbackToken* callbackToken  
)  

パラメーター

device _In_opt_
型: IGameInputDevice*
登録されたコールバックを特定のデバイスのトリガーのみに制限します。
inputKind _In_
型: GameInputKind

登録されたコールバックを、指定された入力の種類の 1 つ以上をサポートするデバイスのトリガーのみに制限します。

statusFilter _In_
型: GameInputDeviceStatus

登録されたコールバックを、特定の種類のデバイス状態変更のトリガーのみに制限します。

enumerationKind _In_
型: GameInputEnumerationKind

デバイスが列挙されるかどうか、また、関数が列挙の完了を待機するかどうかを決定します。

context _In_opt_
型: void*

コールバック関数に関連する情報を提供するいくつかのオブジェクト。 通常は、呼び出し元のオブジェクトです。

callbackFunc _In_
型: GameInputDeviceCallback

デバイスの接続または切断イベントに対して登録するタイトル定義コールバック。

callbackToken _Result_zeroonfailure_
型: GameInputCallbackToken*

登録されているコールバック関数を識別するトークン。 このトークンは、コールバック関数を取り消すか登録解除する必要がある場合に登録されている関数を識別するために使用されます。

戻り値

型: HRESULT

関数の結果です。

解説

IGameInput::RegisterDeviceCallback によって登録される関数を使用して、接続や切断のような一般的なことから、ワイヤード (有線) 入力と接続された入力間の切り替えのようなより特殊なことまで、デバイスの状態の変化に対応できます。 認識可能な状態変化の種類の一覧は、「GameInputDeviceStatus」に記載されています。

enumerationKind パラメーターを使用すると、システムに接続されているデバイスごとに 1 つずつ、初期コールバックをいっせいに生成させることができます。 この初期列挙は、非同期または同期の列挙に設定できます。 同期列挙は、IGameInput::RegisterDeviceCallback が戻る前にすべての初期コールバックを呼び出します。

次の C++ サンプルでは、接続されているゲームパッドおよびキーボードを明示的に列挙する方法を示します。

Microsoft::WRL::ComPtr<IGameInput> gameInput; 

void CALLBACK OnDeviceEnumerated( 
    _In_ GameInputCallbackToken callbackToken, 
    _In_ void * context, 
    _In_ IGameInputDevice * device,
    _In_ uint64_t timestamp, 
    _In_ GameInputDeviceStatus currentStatus, 
    _In_ GameInputDeviceStatus previousStatus) 
{ 
    // Application-specific code to handle the enumerated device 
} 
 
void EnumerateDevicesWorker() noexcept 
{ 
    GameInputCallbackToken token; 
    if (SUCCEEDED(gameInput->RegisterDeviceCallback( 
        nullptr,                                      // Don't filter to events from a specific device 
        GameInputKindGamepad | GameInputKindKeyboard, // Enumerate gamepads and keyboards
        GameInputDeviceAnyStatus,                     // Any device status 
        GameInputBlockingEnumeration,                 // Enumerate synchronously 
        nullptr,                                      // No callback context parameter 
        OnDeviceEnumerated,                           // Callback function 
        &token)))                                     // Generated token 
    { 
        gameInput->UnregisterCallback(token, 5000); 
    }
} 

次の C++ サンプルでは、デバイスが接続または切断されたときに通知を受ける方法を示します。

Microsoft::WRL::ComPtr<IGameInput> gameInput; 
 
void CALLBACK OnDeviceConnectionChanged( 
    _In_ GameInputCallbackToken callbackToken, 
    _In_ void * context, 
    _In_ IGameInputDevice * device, 
    _In_ uint64_t timestamp, 
    _In_ GameInputDeviceStatus currentStatus, 
    _In_ GameInputDeviceStatus previousStatus, 
    ) 
{ 
    if (currentStatus & GameInputDeviceConnected)
    {
        // Application-specific code to handle the device connection 
    }
    else
    {
        // Application-specific code to handle the device disconnection 
    }
} 
 
void MonitorDeviceConnectionChanges( 
    _In_ volatile bool & cancelMonitoring) noexcept 
{ 
    GameInputCallbackToken token; 
    if (SUCCEEDED(gameInput->RegisterDeviceCallback( 
        nullptr,                                      // Don't filter to events from a specific device 
        GameInputKindGamepad | GameInputKindKeyboard, // Listen for Gamepad and Keyboard changes
        GameInputDeviceConnected,                     // Notify on changes to GameInputDeviceConnected status 
        GameInputAsyncEnumeration,                    // Enumerate initial devices asynchronously
        nullptr,                                      // No callback context parameter 
        OnDeviceConnectionChanged,                    // Callback function 
        &token)))                                     // Generated token 
    { 
        while (!cancelMonitoring) 
        { 
            Sleep(100); 
        } 
 
        gameInput->UnregisterCallback(token, 5000); 
    } 
} 

要件

ヘッダー: GameInput.h

ライブラリ: xgameruntime.lib

サポートされているプラットフォーム: Windows、Xbox One ファミリー本体、Xbox Series 本体

関連項目

入力 API の概要
IGameInput
IGameInput::UnregisterCallback
IGameInput::StopCallback