> ## 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

Inscrit une fonction GameInputDeviceCallback à appeler chaque fois qu'un appareil est connecté au système ou déconnecté de celui-ci. Permet également d'appeler la fonction lorsque les propriétés d'un appareil changent.

## Syntaxe

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

### Paramètres

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

Limite le rappel inscrit afin qu'il ne se déclenche que pour un appareil spécifique.

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

Limite le rappel inscrit afin qu'il ne se déclenche que pour les appareils qui prennent en charge au moins un des types d'entrée spécifiés.

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

Limite le rappel inscrit afin qu'il ne se déclenche que pour des types spécifiques de changements d'état de l'appareil.

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

Détermine si les appareils seront énumérés et si la fonction attendra la fin de l'énumération.

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

Un objet qui fournit des renseignements pertinents pour la fonction de rappel. Il s'agit généralement de l'objet appelant.

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

Le rappel défini par le titre à inscrire pour l'événement de connexion ou de déconnexion de l'appareil.

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

Jeton identifiant la fonction de rappel inscrite. Ce jeton sert à identifier la fonction inscrite dans le cas où vous devez annuler ou désinscrire la fonction de rappel.

### Valeur de retour

Type : HRESULT

Résultat de la fonction.

## Remarques

La fonction inscrite par **IGameInput::RegisterDeviceCallback** peut servir à réagir aux changements d'état de l'appareil, comme une connexion ou une déconnexion. Les types de changements d'état reconnaissables sont énumérés dans [GameInputDeviceStatus](/fr-CA/reference/input/gameinput/enums/gameinputdevicestatus).

Le paramètre *enumerationKind* peut servir à générer une salve initiale de rappels, soit un pour chaque appareil connecté au système. Cette énumération initiale peut être définie comme une énumération asynchrone ou synchrone. Une énumération synchrone appellera tous les rappels initiaux avant le retour de **IGameInput::RegisterDeviceCallback**.

L'exemple C++ suivant montre comment énumérer explicitement les manettes et les claviers connectés.

```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);
    }
}
```

L'exemple C++ suivant montre comment être averti lorsqu'un appareil est connecté ou déconnecté.

```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);
    }
}
```

## Configuration requise

[Vue d'ensemble de l'API d'entrée](/fr-CA/build/core-features/common/input/overviews/input-overview)\
[IGameInput](/fr-CA/reference/input/gameinput/interfaces/igameinput/igameinput)\
[IGameInput::UnregisterCallback](/fr-CA/reference/input/gameinput/interfaces/igameinput/methods/igameinput_unregistercallback)\
[IGameInput::StopCallback](/fr-CA/reference/input/gameinput/interfaces/igameinput/methods/igameinput_stopcallback)

## Historique des versions

| Version | Modifications |
| - | - |
| **v0** | Introduit. |


## Related topics

- [IGameInput::RegisterDeviceCallback](/reference/input/gameinput/interfaces/igameinput/methods/igameinput_registerdevicecallback.md)
- [GameInputDeviceCallback](/reference/input/gameinput/functions/gameinputdevicecallback.md)
- [IGameInput::StopCallback](/reference/input/gameinput/interfaces/igameinput/methods/igameinput_stopcallback.md)
