# PointerCaptureComponent

Artifact: [androidx.xr.scenecore:scenecore](https://developer.android.com/jetpack/androidx/releases/xr-scenecore) [View Source](https://cs.android.com/search?q=file:androidx/xr/scenecore/PointerCaptureComponent.kt+class:androidx.xr.scenecore.PointerCaptureComponent) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/xr-scenecore#1.0.0)

*** ** * ** ***


```
public final class PointerCaptureComponent extends Component
```

<br />

|---|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object.html) |||
| ↳ | [androidx.xr.scenecore.Component](https://developer.android.com/reference/androidx/xr/scenecore/Component) ||
|   | ↳ | [androidx.xr.scenecore.PointerCaptureComponent](https://developer.android.com/reference/androidx/xr/scenecore/PointerCaptureComponent) |

*** ** * ** ***

Provides pointer capture capabilities for a given `https://developer.android.com/reference/androidx/xr/scenecore/Entity`.

To enable pointer capture, the task must be in full space, the entity must be visible, and the `https://developer.android.com/reference/androidx/xr/scenecore/PointerCaptureComponent` must be attached to the entity.

Only one PointerCaptureComponent can be attached to an entity at a given time. If a second one tries to attach to an entity, it will fail.

## Summary

| ### Nested types |
|---|
| `public final class https://developer.android.com/reference/androidx/xr/scenecore/PointerCaptureComponent.PointerCaptureState` Defines the possible states of a `https://developer.android.com/reference/androidx/xr/scenecore/PointerCaptureComponent`. |

| ### Public methods |
|---|---|
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/scenecore/PointerCaptureComponent` | `https://developer.android.com/reference/androidx/xr/scenecore/PointerCaptureComponent#create(androidx.xr.runtime.Session,java.util.concurrent.Executor,java.util.function.Consumer,java.util.function.Consumer)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/runtime/Session session, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/concurrent/Executor.html executor, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/function/Consumer.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/scenecore/PointerCaptureComponent.PointerCaptureState> stateListener, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/function/Consumer.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/scenecore/InputEvent> inputListener )` Creates a new instance of `https://developer.android.com/reference/androidx/xr/scenecore/PointerCaptureComponent`. |

| ### Protected methods |
|---|---|
| `boolean` | `https://developer.android.com/reference/androidx/xr/scenecore/PointerCaptureComponent#onAttach(androidx.xr.scenecore.Entity)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/scenecore/Entity entity)` Called by the framework when this component is being added to an `https://developer.android.com/reference/androidx/xr/scenecore/Entity`. |
| `void` | `https://developer.android.com/reference/androidx/xr/scenecore/PointerCaptureComponent#onDetach(androidx.xr.scenecore.Entity)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/scenecore/Entity entity)` Called by the framework when this component is being removed from an `https://developer.android.com/reference/androidx/xr/scenecore/Entity`. |

## Public methods

### create

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/xr-scenecore#1.0.0)

```
public static final @NonNull PointerCaptureComponent create(
    @NonNull Session session,
    @NonNull Executor executor,
    @NonNull Consumer<@NonNull PointerCaptureComponent.PointerCaptureState> stateListener,
    @NonNull Consumer<@NonNull InputEvent> inputListener
)
```

Creates a new instance of `https://developer.android.com/reference/androidx/xr/scenecore/PointerCaptureComponent`.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/runtime/Session session` | The active `https://developer.android.com/reference/androidx/xr/runtime/Session` for the scene. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/concurrent/Executor.html executor` | The `https://developer.android.com/reference/java/util/concurrent/Executor.html` on which the listener callbacks will be invoked. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/function/Consumer.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/scenecore/PointerCaptureComponent.PointerCaptureState> stateListener` | A `https://developer.android.com/reference/java/util/function/Consumer.html` to receive updates when the pointer capture state changes (e.g., from active to paused). |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/function/Consumer.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/scenecore/InputEvent> inputListener` | A `https://developer.android.com/reference/java/util/function/Consumer.html` to receive all `https://developer.android.com/reference/androidx/xr/scenecore/InputEvent`s while pointer capture is active for the attached entity. |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/scenecore/PointerCaptureComponent` | A new instance of `https://developer.android.com/reference/androidx/xr/scenecore/PointerCaptureComponent`. |

## Protected methods

### onAttach

```
protected boolean onAttach(@NonNull Entity entity)
```

Called by the framework when this component is being added to an `https://developer.android.com/reference/androidx/xr/scenecore/Entity`.

This method is triggered when `https://developer.android.com/reference/androidx/xr/scenecore/Entity#addComponent(androidx.xr.scenecore.Component)` is invoked. Implementations should override this method to perform setup logic or to validate if the component is compatible with the provided `https://developer.android.com/reference/androidx/xr/scenecore/Component#onAttach(androidx.xr.scenecore.Entity)`.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/scenecore/Entity entity` | The `https://developer.android.com/reference/androidx/xr/scenecore/Entity` to which this component is being attached. |

| Returns |
|---|---|
| `boolean` | `true` if the component was successfully attached; `false` if the `https://developer.android.com/reference/androidx/xr/scenecore/Component#onAttach(androidx.xr.scenecore.Entity)` does not support this component or if attachment failed. |

### onDetach

```
protected void onDetach(@NonNull Entity entity)
```

Called by the framework when this component is being removed from an `https://developer.android.com/reference/androidx/xr/scenecore/Entity`.

This method is triggered when `https://developer.android.com/reference/androidx/xr/scenecore/Entity#removeComponent(androidx.xr.scenecore.Component)` is invoked. Implementations should override this method to release resources or undo any changes made during `https://developer.android.com/reference/androidx/xr/scenecore/Component#onAttach(androidx.xr.scenecore.Entity)`.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/scenecore/Entity entity` | The `https://developer.android.com/reference/androidx/xr/scenecore/Entity` from which this component is being detached. |