API

Device Source Manager

Introduction

The DeviceSourceManager is a class that manages connections for various user input devices and provides methods for querying those devices for their current state.
This class supports several methods of input:

  • Keyboard (DeviceType: BABYLON.DeviceType.Keyboard, Inputs: number)
  • Mouse (DeviceType: BABYLON.DeviceType.Mouse, Inputs: BABYLON.PointerInput)
  • Touch (DeviceType: BABYLON.DeviceType.Pointer, Inputs: BABYLON.PointerInput)
  • DualShock Gamepad (DeviceType: BABYLON.DeviceType.DualShock, Inputs: BABYLON.DualShockInput)
  • Xbox Gamepad, One or 360 (DeviceType: BABYLON.DeviceType.Xbox, Inputs: BABYLON.XboxInput)
  • Switch Gamepad, L+R JoyCon Grip or Pro Controller (DeviceType: BABYLON.DeviceType.Switch, Inputs: BABYLON.SwitchInput)
  • Generic/Other Gamepad (DeviceType: BABYLON.DeviceType.Generic, Inputs: number)

Here's an example of the DeviceSourceManager in use.

DeviceSourceManager Example

To use the DeviceSourceManager, first create an instance of it. You will need to provide an engine object.

import { DeviceSourceManager } from "@babylonjs/core/DeviceInput/InputDevices/deviceSourceManager";
const deviceSourceManager = new DeviceSourceManager(scene.getEngine());

Within your scene's render/game loop, you can query the DeviceSourceManager for the current state of a specific input. First, you will need to get the DeviceSource object. With this object, you can then query for a specific input's status.

import { DeviceType, XboxInput } from "@babylonjs/core/DeviceInput/InputDevices/deviceEnums";
// If the device has been registered in the DeviceSourceManager
if (deviceSourceManager.getDeviceSource(DeviceType.Xbox)) {
// And the A button was pressed
if (deviceSourceManager.getDeviceSource(DeviceType.Xbox).getInput(XboxInput.A) === 1) {
// Do something
}
}

You can also use optional chaining to fit the checks into a single line.

import { DeviceType, XboxInput } from "@babylonjs/core/DeviceInput/InputDevices/deviceEnums";
if (deviceSourceManager.getDeviceSource(DeviceType.Xbox)?.getInput(XboxInput.A) === 1) {
// Do something
}

Events and Observables

You can use the following Observables to work with a given device's identifiers.

// When a device is registered in the DeviceSourceManager
onDeviceConnectedObservable.add((device) => {
// You can get the device type by using device.deviceType
// You can also get the device slot by using device.deviceSlot (default is 0 for keyboard and 1 for mouse)
// "device" is of Type DeviceSource do you can also access the getInput function or add an observable for onInputChangedObservable
});
// When a device is unregistered in the DeviceSourceManager
onDeviceDisconnectedObservable.add((device) => {
// You can get the device type by using device.deviceType
// You can also get the device slot by using device.deviceSlot (default is 0 for keyboard and 1 for mouse)
// "device" is of Type DeviceSource
});

Here's a demonstration of how these Observables work.

Basic DeviceSourceManager Demo

For keyboards and pointers, you can use an event-based system to get the current and previous input when an input is activated.

import { DeviceType } from "@babylonjs/core/DeviceInput/InputDevices/deviceEnums";
deviceSourceManager.getDeviceSource(DeviceType.Keyboard).onInputChangedObservable.add((eventData) => {
// eventData will contain the event data that you'd find in something like a PointerEvent or KeyboardEvent
// eventData also has an additional value inputIndex
});