Input Class
Helper for binding user key input to actions.
Constructor
Input
-
onKeyAction
-
bindings
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
ArrayInput keys mapped to action1. ['A', 'B', ...]
-
action2
ArrayInput keys mapped to action2. ['X', 'Y', ...]
-
...
Example:
//bindings param example:
{
up: ['UP_ARROW'],
down: ['DOWN_ARROW'],
left: ['LEFT_ARROW'],
right: ['RIGHT_ARROW'],
}
Item Index
Properties
- bindings
- Keys static
- onKeyAction
Methods
addBindings
-
bindings
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
ArrayInput keys mapped to action1. ['A', 'B', ...]
-
action2
ArrayInput 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
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
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
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
()
Binds event listener for document keydown event.
stopListening
()
Unbinds document keydown event listener
Properties
bindings
Object
An object of key val pairs mapping keyCode to action
Keys
Object
static
onKeyAction
Function
Function called when a key bound to an action is pressed (function(action){}).