API Reference

Class List

Controller

A general input handler which handles both mouse and keyboard input assigned to named actions. This allows you to define input handlers separately to defining keyboard/mouse configurations.

const c = new pc.Controller(document);

// Register the "fire" action and assign it to both the Enter key and the space bar.
c.registerKeys("fire", [pc.KEY_ENTER, pc.KEY_SPACE]);

Summary

Methods

appendAction

Helper function to append an action.

attach

Attach Controller to an Element.

detach

Detach Controller from an Element.

disableContextMenu

Disable the context menu usually activated with the right mouse button.

enableContextMenu

Enable the context menu usually activated with the right mouse button.

isPressed

Returns true if the current action is enabled.

registerAxis

Register an action against a controller axis.

registerKeys

Create or update a action which is enabled when the supplied keys are pressed.

registerMouse

Create or update an action which is enabled when the supplied mouse button is pressed.

registerPadButton

Create or update an action which is enabled when the gamepad button is pressed.

update

Update the Keyboard and Mouse handlers.

wasPressed

Returns true if the action was enabled this since the last update.

Details

Constructor

Controller([element], [options])

Create a new instance of a Controller.

const c = new pc.Controller(document);

// Register the "fire" action and assign it to both the Enter key and the space bar.
c.registerKeys("fire", [pc.KEY_ENTER, pc.KEY_SPACE]);

Parameters

elementElement

Element to attach Controller to.

optionsobject

Optional arguments.

options.keyboardKeyboard

A Keyboard object to use.

options.mouseMouse

A Mouse object to use.

options.gamepadsGamePads

A Gamepads object to use.

Methods

appendAction(action_name, action)

Helper function to append an action.

Parameters

action_namestring

The name of the action.

actionobject

An action object to add.

action.typeACTION_KEYBOARD, ACTION_MOUSE, ACTION_GAMEPAD

The name of the action.

action.keysnumber[]

Keyboard: A list of keycodes e.g. [pc.KEY_A, pc.KEY_ENTER].

action.buttonnumber

Mouse: e.g. pc.MOUSEBUTTON_LEFT - Gamepad: e.g. pc.PAD_FACE_1

action.padnumber

Gamepad: An index of the pad to register (use PAD_1, etc).

attach(element)

Attach Controller to an Element. This is required before you can monitor for key/mouse inputs.

Parameters

elementElement

The element to attach mouse and keyboard event handler too.

detach()

Detach Controller from an Element. This should be done before the Controller is destroyed.

disableContextMenu()

Disable the context menu usually activated with the right mouse button.

enableContextMenu()

Enable the context menu usually activated with the right mouse button. This is enabled by default.

isPressed(actionName)

Returns true if the current action is enabled.

Parameters

actionNamestring

The name of the action.

Returns

boolean

True if the action is enabled.

registerAxis([options])

Register an action against a controller axis.

Parameters

optionsobject

Optional options object.

options.padnumber

The index of the game pad to register for (use PAD_1, etc).

registerKeys(action, keys)

Create or update a action which is enabled when the supplied keys are pressed.

Parameters

actionstring

The name of the action.

keysnumber[]

A list of keycodes.

registerMouse(action, button)

Create or update an action which is enabled when the supplied mouse button is pressed.

Parameters

actionstring

The name of the action.

buttonnumber

The mouse button.

registerPadButton(action, pad, button)

Create or update an action which is enabled when the gamepad button is pressed.

Parameters

actionstring

The name of the action.

padnumber

The index of the pad to register (use PAD_1, etc).

buttonnumber

The pad button.

update(dt)

Update the Keyboard and Mouse handlers.

Parameters

dtobject

The time since the last frame.

wasPressed(actionName)

Returns true if the action was enabled this since the last update.

Parameters

actionNamestring

The name of the action.

Returns

boolean

True if the action was enabled this since the last update.