API Docs for:
Show:

Input Class

Defined in: src/input.js:4

Helper for binding user key input to actions.

Constructor

Input

(
  • onKeyAction
  • bindings
)

Defined in src/input.js:4

Parameters:

  • onKeyAction Function
    • Function called when a key bound to an action is pressed (function(action){}).
  • bindings Object
    • An object of key val pairs mapping an action to an array of keys that trigger it. Input.Keys is used to convert input key string names to key codes.
    • action1 Array

      Input keys mapped to action1. ['A', 'B', ...]

    • action2 Array

      Input keys mapped to action2. ['X', 'Y', ...]

    • ...

Example:

   //bindings param example:
   {
       up: ['UP_ARROW'],
       down: ['DOWN_ARROW'],
       left: ['LEFT_ARROW'],
       right: ['RIGHT_ARROW'],
   }

Methods

addBindings

(
  • bindings
)

Defined in src/input.js:75

Loads multiple action key bindings

Parameters:

  • bindings Object
    • An object of key val pairs mapping an action to an array of keys that trigger it. Input.Keys is used to convert input key string names to key codes.
    • action1 Array

      Input keys mapped to action1. ['A', 'B', ...]

    • action2 Array

      Input keys mapped to action2. ['X', 'Y', ...]

    • ...

Example:

       //bindings param example:
       {
           up: ['UP_ARROW'],
           down: ['DOWN_ARROW'],
           left: ['LEFT_ARROW'],
           right: ['RIGHT_ARROW'],
       }

bindAction

(
  • action
  • keyName
)

Defined in src/input.js:50

Bind a user action to a key input

Parameters:

  • action String
    • The action to bind the key to.
  • keyName String
    • The keyname to bind the action to. @see Input.Keys

getActionFromKeyCode

(
  • keyCode
)

Defined in src/input.js:61

Converts a user input key code to an action bound to that key or false if none is bound.

Parameters:

  • keyCode String
    • The key code to retrieve an action bound to.

Returns:

String|bool The action bound to the keyCode or false.

handleEvent

(
  • event
)

Defined in src/input.js:101

Event handler for document.addEventListener('keydown', this). Triggered when a key is pressed. If an action is bound to the key pressed, false is returned to cancel the default browser behavior of the keypress. If an action is not bound to the key pressed, true is returned allowing the default browser behavior of the keypress to function.

Parameters:

  • event Object
    • The onkeydown event.

Returns:

bool

startListening

()

Defined in src/input.js:128

Binds event listener for document keydown event.

stopListening

()

Defined in src/input.js:136

Unbinds document keydown event listener

Properties

bindings

Object

Defined in src/input.js:36

An object of key val pairs mapping keyCode to action

Keys

Object static

Defined in src/input.js:145

onKeyAction

Function

Defined in src/input.js:43

Function called when a key bound to an action is pressed (function(action){}).