> ## 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 périphérique est connecté au système ou déconnecté de celui-ci. Permet également d'appeler la fonction lorsque les propriétés d'un périphérique 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 déclenchement du rappel inscrit à un périphérique spécifique.

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

Limite le déclenchement du rappel inscrit aux périphériques qui prennent en charge au moins l'un des types d'entrée spécifiés.

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

Limite le déclenchement du rappel inscrit à des types spécifiques de changements d'état du périphérique.

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

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

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

Objet qui fournit des informations pertinentes pour la fonction de rappel. Il s'agit généralement de l'objet appelant.

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

Rappel défini par le titre à inscrire pour l'événement de connexion ou de déconnexion du périphérique.

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

Jeton identifiant la fonction de rappel inscrite. Ce jeton permet d'identifier la fonction inscrite au cas où vous devriez 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 être utilisée pour réagir aux changements d'état du périphérique, comme une connexion ou une déconnexion. Les types de changements d'état reconnaissables sont répertoriés dans [GameInputDeviceStatus](/fr/reference/input/gameinput/enums/gameinputdevicestatus).

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

L'exemple C++ suivant montre comment énumérer explicitement les manettes de jeu 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 notifié lorsqu'un périphérique 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/build/core-features/common/input/overviews/input-overview)\
[IGameInput](/fr/reference/input/gameinput/interfaces/igameinput/igameinput)\
[IGameInput::UnregisterCallback](/fr/reference/input/gameinput/interfaces/igameinput/methods/igameinput_unregistercallback)\
[IGameInput::StopCallback](/fr/reference/input/gameinput/interfaces/igameinput/methods/igameinput_stopcallback)

## Historique des versions

| Version | Modifications |
| - | - |
| **v0** | Introduite. |


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