注册在设备连接到系统或者与系统断开连接时要调用的 GameInputDeviceCallback 函数。 还允许在设备的属性更改时要调用的函数。
语法
HRESULT RegisterDeviceCallback(
IGameInputDevice* device,
GameInputKind inputKind,
GameInputDeviceStatus statusFilter,
GameInputEnumerationKind enumerationKind,
void* context,
GameInputDeviceCallback callbackFunc,
GameInputCallbackToken* callbackToken
)
参数
device _In_opt_
类型:IGameInputDevice*
将注册的回调限制为仅为某一特定设备触发。
inputKind _In_
类型:GameInputKind
将注册的回调限制为仅为支持至少一种指定的输入类型的设备触发。
statusFilter _In_
类型:GameInputDeviceStatus
将注册的回调限制为仅为特定类型的设备状态更改触发。
enumerationKind _In_
类型:GameInputEnumerationKind
确定是否将枚举设备,以及该函数是否将等待枚举完成。
context _In_opt_
类型:void*
提供有关回调函数的相关信息的某些对象。 通常的调用对象。
callbackFunc _In_
类型:GameInputDeviceCallback
游戏定义回调以便注册设备连接或断开连接的事件。
callbackToken _Result_zeroonfailure_
类型:GameInputCallbackToken*
标识注册的回调函数的令牌。 此令牌将用于在需要取消或注销回调函数时标识已注册的函数。
返回值
类型:HRESULT
函数结果。
备注
IGameInput::RegisterDeviceCallback 注册的函数可用于对设备状态中的变化(通常是连接或断开连接,也可以是更具体的变化,例如在有线输入和已连接输入之间切换)作出响应。 可识别的状态变化类型在 GameInputDeviceStatus 中列出。
可以使用 enumerationKind 参数来生成初始回调 - 为连接到系统的每个设备都都生成一个回调。 可将初始枚举设置为异步枚举或同步枚举。 同步枚举将在 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