Class: Keys

controls~Keys(options)

Unified keyboard and gamepad input for desktop games. Puts wasd, cursors and gamepad controls together in a single api. All you need is to check the following properties of this plugin in your scenes update function: up, down, left, right, a, b, x and y.

Constructor

new Keys(options)

Parameters:
Name Type Description
options object An options object. Can be passed down with the game config.
Properties
Name Type Attributes Default Description
wasd boolean <optional>
true Wether or not to support wasd keys.
cursors boolean <optional>
true Wether or not to support cursor or arrow keys.
Source:
Fires:

Extends

Members

a :boolean

Wether the space bar or gamepad A button is currently down.
Type:
  • boolean
Source:

b :boolean

Wether the C or B key or gamepad B button is currently down.
Type:
  • boolean
Source:

down :boolean

Wether the down cursor, S key or dpad down is currently down.
Type:
  • boolean
Source:

engine :module:core~Engine

A reference to the main engine.
Type:
Overrides:
Source:

gamepadButtons :Array.<boolean>

Indicates which gamepad buttons are down
Type:
  • Array.<boolean>
Source:

left :boolean

Wether the left cursor, A or Q key or dpad left is currently down.
Type:
  • boolean
Source:

listeners :object

A dictionary of the event callbacks attached to this object.
Type:
  • object
Overrides:
Source:

name :string

The name you gave this instance of the ScenePlugin in the game config. If you gave the plugin the name `whiskymixer`, it will become available in the scene as property like in `this.whiskymixer`.
Type:
  • string
Overrides:
Source:
Wether the right cursor, D key or dpad right is currently down.
Type:
  • boolean
Source:

scene :module:core~Scene

A reference to the scene where this plugin is active.
Type:
Overrides:
Source:

up :boolean

Wether the up cursor, W or Z key or dpad up is currently down.
Type:
  • boolean
Source:

x :boolean

Wether the X key or gamepad X button is currently down.
Type:
  • boolean
Source:

y :boolean

Wether the Y or R key or gamepad Y button is currently down.
Type:
  • boolean
Source:

Methods

emit(type, eopt)

Emits the event of a certain type and executes all callbacks.
Parameters:
Name Type Attributes Description
type string The name of the event it emits.
e object <optional>
An optional object that will be passed to the callbacks first argument.
Overrides:
Source:

init()

Adds the necessary keyboard and gamepad event listeners at the start of the scene.
Overrides:
Source:

off(typeopt, callbackopt)

Removes listeners from this object.
Parameters:
Name Type Attributes Description
type string <optional>
The name of the event to remove callback(s) for. If left empty, it will remove every single callback from this object.
callback function <optional>
The callback to remove. If left empty, it will remove all callbacks for the given type or when the type is empty every single callback on this object.
Overrides:
Source:

on(type, callback)

Continually listen on this object for an event.
Parameters:
Name Type Description
type string The name of the event.
callback function The function to execute each time the event is emitted.
Overrides:
Source:

once(type, callback)

Listen on this object for an event, but execute the callback only once.
Parameters:
Name Type Description
type string The name of the event.
callback function The function to execute once.
Overrides:
Source:

onGamepadConnected(e)

Called internally when a gamepad is connected.
Parameters:
Name Type Description
e object The native javascript event object.
Source:

onGamepadDisconnected(e)

Called internally when a gamepad is disconnected.
Parameters:
Name Type Description
e object The native javascript event object.
Source:

onKeyDown(e)

Called internally when a key goes down. Override this if you want to support more keys.
Parameters:
Name Type Description
e object The native javascript event object.
Source:

onKeyUp(e)

Called internally when a key goes up. Override this if you want to support more keys.
Parameters:
Name Type Description
e object The native javascript event object.
Source:

(abstract) postRender(context, time, delta)

Called on each frame if the scene is active. After all children are rendered. No need to call `super.postRender(context, time, delta)`
Parameters:
Name Type Description
context CanvasRenderingContext2D The canvas render context.
time number The total time (in milliesconds) since the start of the game.
delta number The time elapsed (in milliseconds) since the last frame.
Overrides:
Source:

shutdown()

Removes all the keyboard and gamepad event listeners when the scene shuts down.
Overrides:
Source:

update(time, delta)

Called internally to update the gamepad mappings. Override this if you want to support more buttons of the gamepad.
Parameters:
Name Type Description
time number The total time (in milliesconds) since the start of the game.
delta number The time elapsed (in milliseconds) since the last frame.
Overrides:
Source:

Events

keydown

Any key goes down. Passes the native KeyboardEvent.
Type:
  • KeyboardEvent
Source:

keyup

Any key goes up. Passes the native KeyboardEvent.
Type:
  • KeyboardEvent
Source:

paddown

Gamepad button goes down. Passes {id, name} of the button.
Type:
  • object
Source:

padup

Gamepad button goes up. Passes {id, name} of the button.
Type:
  • object
Source: