# XrInputSource

Class · extends [`EventHandler`](https://api.playcanvas.com/engine/classes/EventHandler.md) · category: XR

Source: https://github.com/playcanvas/engine/blob/b5b983982a9860d21e0c1dafb2f85f72e2c01afb/src/framework/xr/xr-input-source.js#L29

Represents XR input source, which is any input mechanism which allows the user to perform
targeted actions in the same virtual space as the viewer. Example XR input sources include, but
are not limited to: handheld controllers, optically tracked hands, touch screen taps, and
gaze-based input methods that operate on the viewer's pose.

## Accessors

### elementEntity

```ts
get elementEntity(): Entity | null
```

If [elementInput](https://api.playcanvas.com/engine/classes/XrInputSource.md#elementinput) is true, this property will hold entity with Element
component at which this input source is hovering, or null if not hovering over any element.

### elementInput

```ts
get elementInput(): boolean
set elementInput(value: boolean)
```

Gets whether the input source can interact with [ElementComponent](https://api.playcanvas.com/engine/classes/ElementComponent.md)s.

### gamepad

```ts
get gamepad(): Gamepad | null
```

If input source has buttons, triggers, thumbstick or touchpad, then this object provides
access to its states.

### grip

```ts
get grip(): boolean
```

If input source can be held, then it will have node with its world transformation, that can
be used to position and rotate visual object based on it.

### hand

```ts
get hand(): XrHand | null
```

If input source is a tracked hand, then it will point to [XrHand](https://api.playcanvas.com/engine/classes/XrHand.md) otherwise it is
null.

### handedness

```ts
get handedness(): string
```

Describes which hand input source is associated with. Can be one of the following:

- [XRHAND_NONE](https://api.playcanvas.com/engine/variables/XRHAND_NONE.md): None - input source is not meant to be held in hands.
- [XRHAND_LEFT](https://api.playcanvas.com/engine/variables/XRHAND_LEFT.md): Left - indicates that input source is meant to be held in left hand.
- [XRHAND_RIGHT](https://api.playcanvas.com/engine/variables/XRHAND_RIGHT.md): Right - indicates that input source is meant to be held in right
hand.

### hitTestSources

```ts
get hitTestSources(): XrHitTestSource[]
```

List of active [XrHitTestSource](https://api.playcanvas.com/engine/classes/XrHitTestSource.md) instances associated with this input source.

### id

```ts
get id(): number
```

Unique number associated with instance of input source. Same physical devices when
reconnected will not share this ID.

### inputSource

```ts
get inputSource(): XRInputSource
```

XRInputSource object that is associated with this input source.

### profiles

```ts
get profiles(): string[]
```

List of input profile names indicating both the preferred visual representation and behavior
of the input source.

### selecting

```ts
get selecting(): boolean
```

True if input source is in active primary action between selectstart and selectend events.

### squeezing

```ts
get squeezing(): boolean
```

True if input source is in active squeeze action between squeezestart and squeezeend events.

### targetRayMode

```ts
get targetRayMode(): string
```

Type of ray Input Device is based on. Can be one of the following:

- [XRTARGETRAY_GAZE](https://api.playcanvas.com/engine/variables/XRTARGETRAY_GAZE.md): Gaze - indicates the target ray will originate at the viewer and
follow the direction it is facing. This is commonly referred to as a "gaze input" device in
the context of head-mounted displays.
- [XRTARGETRAY_SCREEN](https://api.playcanvas.com/engine/variables/XRTARGETRAY_SCREEN.md): Screen - indicates that the input source was an interaction
with the canvas element associated with an inline session's output context, such as a mouse
click or touch event.
- [XRTARGETRAY_POINTER](https://api.playcanvas.com/engine/variables/XRTARGETRAY_POINTER.md): Tracked Pointer - indicates that the target ray originates
from either a handheld device or other hand-tracking mechanism and represents that the user
is using their hands or the held device for pointing.

## Methods

### getDirection

```ts
getDirection(): Vec3
```

Get the world space direction of input source ray.

**Returns** [`Vec3`](https://api.playcanvas.com/engine/classes/Vec3.md): The world space direction of input source ray.

### getLinearVelocity

```ts
getLinearVelocity(): Vec3 | null
```

Get the linear velocity (units per second) of the input source if it is handheld
([grip](https://api.playcanvas.com/engine/classes/XrInputSource.md#grip) is true). Otherwise it will return null.

**Returns** [`Vec3`](https://api.playcanvas.com/engine/classes/Vec3.md) `| null`: The world space linear velocity of the handheld input source.

### getLocalPosition

```ts
getLocalPosition(): Vec3 | null
```

Get the local space position of input source if it is handheld ([grip](https://api.playcanvas.com/engine/classes/XrInputSource.md#grip) is true). Local
space is relative to parent of the XR camera. Otherwise it will return null.

**Returns** [`Vec3`](https://api.playcanvas.com/engine/classes/Vec3.md) `| null`: The local space position of handheld input source.

### getLocalRotation

```ts
getLocalRotation(): Quat | null
```

Get the local space rotation of input source if it is handheld ([grip](https://api.playcanvas.com/engine/classes/XrInputSource.md#grip) is true). Local
space is relative to parent of the XR camera. Otherwise it will return null.

**Returns** [`Quat`](https://api.playcanvas.com/engine/classes/Quat.md) `| null`: The local space rotation of handheld input source.

### getOrigin

```ts
getOrigin(): Vec3
```

Get the world space origin of input source ray.

**Returns** [`Vec3`](https://api.playcanvas.com/engine/classes/Vec3.md): The world space origin of input source ray.

### getPosition

```ts
getPosition(): Vec3 | null
```

Get the world space position of input source if it is handheld ([grip](https://api.playcanvas.com/engine/classes/XrInputSource.md#grip) is true).
Otherwise it will return null.

**Returns** [`Vec3`](https://api.playcanvas.com/engine/classes/Vec3.md) `| null`: The world space position of handheld input source.

### getRotation

```ts
getRotation(): Quat | null
```

Get the world space rotation of input source if it is handheld ([grip](https://api.playcanvas.com/engine/classes/XrInputSource.md#grip) is true).
Otherwise it will return null.

**Returns** [`Quat`](https://api.playcanvas.com/engine/classes/Quat.md) `| null`: The world space rotation of handheld input source.

### hitTestStart

```ts
hitTestStart(options?: object): void
```

Attempts to start hit test source based on this input source.

**Parameters**

- `options` (`object`, optional, default `{}`): Object for passing optional arguments.
    - `options.callback` ([`XrHitTestStartCallback`](https://api.playcanvas.com/engine/types/XrHitTestStartCallback.md), optional): Optional callback function called once
      hit test source is created or failed.
    - `options.entityTypes` (`string[]`, optional): Optional list of underlying entity types against
      which hit tests will be performed. Defaults to [[XRTRACKABLE_PLANE](https://api.playcanvas.com/engine/variables/XRTRACKABLE_PLANE.md)]. Can be any
      combination of the following:

      - [XRTRACKABLE_POINT](https://api.playcanvas.com/engine/variables/XRTRACKABLE_POINT.md): Point - indicates that the hit test results will be computed
      based on the feature points detected by the underlying Augmented Reality system.
      - [XRTRACKABLE_PLANE](https://api.playcanvas.com/engine/variables/XRTRACKABLE_PLANE.md): Plane - indicates that the hit test results will be computed
      based on the planes detected by the underlying Augmented Reality system.
      - [XRTRACKABLE_MESH](https://api.playcanvas.com/engine/variables/XRTRACKABLE_MESH.md): Mesh - indicates that the hit test results will be computed
      based on the meshes detected by the underlying Augmented Reality system.
    - `options.offsetRay` ([`Ray`](https://api.playcanvas.com/engine/classes/Ray.md), optional): Optional ray by which hit test ray can be offset.

**Example**

```ts
app.xr.input.on('add', (inputSource) => {
    inputSource.hitTestStart({
        callback: (err, hitTestSource) => {
            if (err) return;
            hitTestSource.on('result', (position, rotation, inputSource, hitTestResult) => {
                // position and rotation of hit test result
                // that will be created from touch on mobile devices
            });
        }
    });
});
```

## Events

### EVENT_HITTESTADD

```ts
static EVENT_HITTESTADD: string = 'hittest:add'
```

Fired when new [XrHitTestSource](https://api.playcanvas.com/engine/classes/XrHitTestSource.md) is added to the input source. The handler is passed
the [XrHitTestSource](https://api.playcanvas.com/engine/classes/XrHitTestSource.md) object that has been added.

**Example**

```ts
inputSource.on('hittest:add', (hitTestSource) => {
    // new hit test source is added
});
```

### EVENT_HITTESTREMOVE

```ts
static EVENT_HITTESTREMOVE: string = 'hittest:remove'
```

Fired when [XrHitTestSource](https://api.playcanvas.com/engine/classes/XrHitTestSource.md) is removed from the input source. The handler is passed
the [XrHitTestSource](https://api.playcanvas.com/engine/classes/XrHitTestSource.md) object that has been removed.

**Example**

```ts
inputSource.on('hittest:remove', (hitTestSource) => {
    // hit test source is removed
});
```

### EVENT_HITTESTRESULT

```ts
static EVENT_HITTESTRESULT: string = 'hittest:result'
```

Fired when hit test source receives new results. It provides transform information that
tries to match real world picked geometry. The handler is passed the [XrHitTestSource](https://api.playcanvas.com/engine/classes/XrHitTestSource.md)
object that produced the hit result, the [Vec3](https://api.playcanvas.com/engine/classes/Vec3.md) position, the [Quat](https://api.playcanvas.com/engine/classes/Quat.md)
rotation and the [XRHitTestResult](https://developer.mozilla.org/en-US/docs/Web/API/XRHitTestResult)
object that is created by the WebXR API.

**Example**

```ts
inputSource.on('hittest:result', (hitTestSource, position, rotation, hitTestResult) => {
    target.setPosition(position);
    target.setRotation(rotation);
});
```

### EVENT_REMOVE

```ts
static EVENT_REMOVE: string = 'remove'
```

Fired when [XrInputSource](https://api.playcanvas.com/engine/classes/XrInputSource.md) is removed.

**Example**

```ts
inputSource.once('remove', () => {
    // input source is not available anymore
});
```

### EVENT_SELECT

```ts
static EVENT_SELECT: string = 'select'
```

Fired when input source has triggered primary action. This could be pressing a trigger
button, or touching a screen. The handler is passed an
[XRInputSourceEvent](https://developer.mozilla.org/en-US/docs/Web/API/XRInputSourceEvent)
object from the WebXR API.

**Example**

```ts
const ray = new Ray();
inputSource.on('select', (evt) => {
    ray.set(inputSource.getOrigin(), inputSource.getDirection());
    if (obj.intersectsRay(ray)) {
        // selected an object with input source
    }
});
```

### EVENT_SELECTEND

```ts
static EVENT_SELECTEND: string = 'selectend'
```

Fired when input source has ended triggering primary action. The handler is passed an
[XRInputSourceEvent](https://developer.mozilla.org/en-US/docs/Web/API/XRInputSourceEvent)
object from the WebXR API.

**Example**

```ts
inputSource.on('selectend', (evt) => {
    console.log('Select ended');
});
```

### EVENT_SELECTSTART

```ts
static EVENT_SELECTSTART: string = 'selectstart'
```

Fired when input source has started to trigger primary action. The handler is passed an
[XRInputSourceEvent](https://developer.mozilla.org/en-US/docs/Web/API/XRInputSourceEvent)
object from the WebXR API.

**Example**

```ts
inputSource.on('selectstart', (evt) => {
    console.log('Select started');
});
```

### EVENT_SQUEEZE

```ts
static EVENT_SQUEEZE: string = 'squeeze'
```

Fired when input source has triggered squeeze action. This is associated with "grabbing"
action on the controllers. The handler is passed an
[XRInputSourceEvent](https://developer.mozilla.org/en-US/docs/Web/API/XRInputSourceEvent)
object from the WebXR API.

**Example**

```ts
inputSource.on('squeeze', (evt) => {
    console.log('Squeeze');
});
```

### EVENT_SQUEEZEEND

```ts
static EVENT_SQUEEZEEND: string = 'squeezeend'
```

Fired when input source has ended triggering squeeze action. The handler is passed an
[XRInputSourceEvent](https://developer.mozilla.org/en-US/docs/Web/API/XRInputSourceEvent)
object from the WebXR API.

**Example**

```ts
inputSource.on('squeezeend', (evt) => {
    console.log('Squeeze ended');
});
```

### EVENT_SQUEEZESTART

```ts
static EVENT_SQUEEZESTART: string = 'squeezestart'
```

Fired when input source has started to trigger squeeze action. The handler is passed an
[XRInputSourceEvent](https://developer.mozilla.org/en-US/docs/Web/API/XRInputSourceEvent)
object from the WebXR API.

**Example**

```ts
inputSource.on('squeezestart', (evt) => {
    if (obj.containsPoint(inputSource.getPosition())) {
        // grabbed an object
    }
});
```

## Inherited from [EventHandler](https://api.playcanvas.com/engine/classes/EventHandler.md)

- `fire(name: string, arg1?: any, arg2?: any, arg3?: any, arg4?: any, arg5?: any, arg6?: any, arg7?: any, arg8?: any): EventHandler`
- `hasEvent(name: string): boolean`
- `off(name?: string, callback?: HandleEventCallback, scope?: any): EventHandler`
- `on(name: string, callback: HandleEventCallback, scope?: any): EventHandle`
- `once(name: string, callback: HandleEventCallback, scope?: any): EventHandle`
