Foundation.KeyController
Event-driven reaction to key events is not always the most convenient way of working when all other game logic is wrapped in a linear loop. If you're happy using event-driven reactions for key events, you can use standard event listeners. However, if you prefer to deal with key events in loop updates, the KeyController type provides a convenient way of doing so.
Internally, key events are buffered per update. The result is that checking if a given key is down (or up, etc.) is actually a check to see if the key was down at any time in the current update.
Key code associations can be found in the KeyCodes type.
Example
// In some object's update method, using the key controller instance
// from the default game prefab (stored as 'game')
if (game['keys'].down(Foundation.KeyCodes.a)){
console.log("The 'a' key was down in the current update");
}
Implementation Details
Reference paths
'/foundation/_digest.ts''/foundation/core/actors/_digest.ts''/foundation/core/actors/KeyController.ts'
Extends
This type extends:
equals() calls will always be false.
clone() calls return null.
free() releases all internal event listeners
Implements
This type does not implement any interfaces (except those in the base).
Constructors
()
- Default, no argument constructor.
Instance Methods
anyDown(): boolean
- Returns true if any key has been detected down in the current update
down(keycode: number)
- Returns true if the passed keycode has been detected down in the current update.
firstDown(keycode: number): boolean
- Returns true if the passed keycode has been detected down in the current update, but not in the previous update.
up(keycode: number)
- Returns true if the passed keycode has been detected up in the current update.
firstUp(keycode: number): boolean
- Returns true if the passed keycode has been detected up in the current update, but not in the previous update.