> ## Documentation Index
> Fetch the complete documentation index at: https://devdocs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# IGameInput::RegisterDeviceCallback

> IGameInput::RegisterDeviceCallback

# IGameInput::RegisterDeviceCallback

Registers a GameInputDeviceCallback function to be called whenever a device is connected or disconnected from the system. Also allows the function to be called when the properties of a device change.

## Syntax

```cpp theme={null}
HRESULT RegisterDeviceCallback(
    IGameInputDevice* device,
    GameInputKind inputKind,
    GameInputDeviceStatus statusFilter,
    GameInputEnumerationKind enumerationKind,
    void* context,
    GameInputDeviceCallback callbackFunc,
    GameInputCallbackToken* callbackToken
);
```

### Parameters

*device*   \_In\_opt\_\
Type: IGameInputDevice\*

Limits registered callback to only trigger for a specific device.

*inputKind*   \_In\_\
Type: [GameInputKind](/reference/input/gameinput/enums/gameinputkind)

Limits registered callback to only trigger for devices that support at least one of the specified types of input.

*statusFilter*   \_In\_\
Type: [GameInputDeviceStatus](/reference/input/gameinput/enums/gameinputdevicestatus)

Limits registered callback to only trigger for specific types of device status changes.

*enumerationKind*   \_In\_\
Type: [GameInputEnumerationKind](/reference/input/gameinput/enums/gameinputenumerationkind)

Determines whether devices will be enumerated and if the function will wait for enumeration to complete.

*context*   \_In\_opt\_\
Type: void\*

Some object which provides relevant information for the callback function. Typically the calling object.

*callbackFunc*   \_In\_\
Type: [GameInputDeviceCallback](/reference/input/gameinput/functions/gameinputdevicecallback)

The title-defined callback to register for the device connected or disconnected event.

*callbackToken*   \_Result\_zeroonfailure\_\
Type: GameInputCallbackToken\*

Token identifying the registered callback function. This token is used to identify the registered function in the event that you need to cancel or unregister the callback function.

### Return value

Type: HRESULT

Function result.

## Remarks

The function registered by **IGameInput::RegisterDeviceCallback** can be used to react to changes of state in the device, such as a connect or disconnect. The types of recognizable state changes are listed in [GameInputDeviceStatus](/reference/input/gameinput/enums/gameinputdevicestatus).

The *enumerationKind* parameter can be used to cause an initial salvo of callbacks to be generated - one for each device that is connected to the system. This initial enumeration can be set to an asynchronous or synchronous enumeration. A synchronous enumeration will call all initial callbacks before **IGameInput::RegisterDeviceCallback** returns.

The following C++ sample demonstrates how to explicitly enumerate connected gamepads and keyboards.

```cpp theme={null}
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);
    }
}
```

The following C++ sample demonstrates how to be notified when a device is connected or disconnected.

```cpp theme={null}
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);
    }
}
```

## Requirements

[Input API Overview](/build/core-features/common/input/overviews/input-overview)\
[IGameInput](/reference/input/gameinput/interfaces/igameinput/igameinput)\
[IGameInput::UnregisterCallback](/reference/input/gameinput/interfaces/igameinput/methods/igameinput_unregistercallback)\
[IGameInput::StopCallback](/reference/input/gameinput/interfaces/igameinput/methods/igameinput_stopcallback)

## Version History

| Version | Changes     |
| ------- | ----------- |
| **v0**  | Introduced. |


## Related topics

- [GameInputDeviceCallback](/reference/input/gameinput/functions/gameinputdevicecallback.md)
- [IGameInput::StopCallback](/reference/input/gameinput/interfaces/igameinput/methods/igameinput_stopcallback.md)
- [IGameInput::UnregisterCallback](/reference/input/gameinput/interfaces/igameinput/methods/igameinput_unregistercallback.md)
- [GameInputDeviceStatus](/reference/input/gameinput/enums/gameinputdevicestatus.md)
- [GameInputEnumerationKind](/reference/input/gameinput/enums/gameinputenumerationkind.md)
