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
element | Element | Element to attach Controller to. |
options | object | Optional arguments. |
options.keyboard | Keyboard | A Keyboard object to use. |
options.mouse | Mouse | A Mouse object to use. |
options.gamepads | GamePads | A Gamepads object to use. |
Methods
appendAction(action_name, action)
Helper function to append an action.
Parameters
action_name | string | The name of the action. |
action | object | An action object to add. |
action.type | ACTION_KEYBOARD, ACTION_MOUSE, ACTION_GAMEPAD | The name of the action. |
action.keys | number[] | Keyboard: A list of keycodes e.g. |
action.button | number | Mouse: e.g. |
action.pad | number | 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
element | Element | 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
actionName | string | The name of the action. |
Returns
booleanTrue if the action is enabled.
registerAxis([options])
Register an action against a controller axis.
Parameters
options | object | Optional options object. |
options.pad | number | 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
action | string | The name of the action. |
keys | number[] | A list of keycodes. |
registerMouse(action, button)
Create or update an action which is enabled when the supplied mouse button is pressed.
Parameters
action | string | The name of the action. |
button | number | The mouse button. |
registerPadButton(action, pad, button)
Create or update an action which is enabled when the gamepad button is pressed.
Parameters
action | string | The name of the action. |
pad | number | The index of the pad to register (use PAD_1, etc). |
button | number | The pad button. |