KeyboardPlugin

View source

ExtendsPlugin

Adds keyboard input support to an App.

KeyboardPlugin provides a Keyboard resource in the app’s default world. Read it to query held KeyCode values and detect keys that were just pressed or released.

The plugin consumes typed KeyDown and KeyUp events from WindowPlugin. Recognized browser code values map to KeyCode values; unsupported codes use KeyCode.Unidentified. KeyboardPlugin does not attach browser listeners itself. In a browser application, add DOMWindowPlugin to bridge native keyboard events into the window event resources.

KeyboardPlugin requires CorePlugin, ReflectPlugin, and WindowPlugin. Register those dependencies before App#run; the app validates dependencies but does not install them automatically.

Examples

import { App } from '@wimaengine/app'
import { CorePlugin } from '@wimaengine/core'
import { Keyboard, KeyboardPlugin, KeyCode } from '@wimaengine/keyboard'
import { ReflectPlugin } from '@wimaengine/reflect'
import { DOMWindowPlugin } from '@wimaengine/window-dom'
import { WindowPlugin } from '@wimaengine/window'

const app = new App()
  .registerPlugin(new ReflectPlugin())
  .registerPlugin(new CorePlugin())
  .registerPlugin(new WindowPlugin())
  .registerPlugin(new KeyboardPlugin())
  .registerPlugin(new DOMWindowPlugin())

app.run()

const keyboard = app.getWorld().getResource(Keyboard)
if (keyboard.justPressed(KeyCode.Space)) {
  console.log('Space started')
}

if (keyboard.pressed(KeyCode.ArrowLeft)) {
  console.log('Move left')
}

Constructor


constructor()

Methods


register(app: App): void

View source

Adds Keyboard to the app’s default world and updates its held and transition state from KeyDown and KeyUp events.

Parameters

NameDescriptionDefault
app

App to configure.

—

Returns

This method updates the app configuration in place.