Skip to content

The gameControl object

Alvaro Montoro edited this page Sep 2, 2019 · 3 revisions

After importing the library into your webpage/project, gameControl will be available to use. This object comes with a series of properties and methods that will allow to handle the different gamepads connected to the computer.

Properties

Name Type Description
isReady Boolean Specifies if the gameControl object is ready and listening for gamepads, or managing already connected ones

Methods

Name and Parameters Description
.on(EVENTNAME, CALLBACK) Associates a function (callback) to be executed when the specified event is triggered (see list of events below).
Example of use:

gameControl.on("connect", function() {
  console.log("gamepad connected!")
})

At most one action can be associated to each event at any given moment.
.off(EVENTNAME) Deassociates the event handler, so no action is performed when the event is triggered.
Example of use:

gameControl.off("connect");
.getGamepad(ID) Returns the gamepad associated to the provided ID or null if none exist. Note that this is not the browser's gamepad, but a gamepad object with different properties and methods (see below section).
Example of use:

const myGamepad = gameControl.getGamepad(0);
.getGamepads() Returns a list of the gamepads connected to the browser and managed by gameControl. Note that these are not browser's gamepads. but instance of gamepad with different properties and methods (see below section).
Example of use:

gameControl.getGamepads();
.set(PROPERTY, VALUE) Sets the value of a property. This property will be inherited by all the gamepads. At the moment, only the axeThreshold property can be used with this method.
Example of use:

gameControl.set('axeThreshold', 0.5);

Associated Events

This is a list of the events that can be associated to gameController itself.

Name Description
connect Triggered every time that a gamepad is connected to the browser. It returns an instance of the `gamepad` object described below.
disconnect Triggered when a gamepad is disconnected from the browser.
beforeCycle Triggered before the gamepads are checked for pressed buttons/joysticks movement (before those events are triggered).
afterCycle Triggered after the gamepads are checked for pressed buttons/joysticks movement (after those events have been triggered).