Input

A wrapper for initializing and handling multiple inputs at once (keyboard, gamepad, gesture, pointer).

let { initInput, onInput } = kontra;

// this function must be called first before input
// functions will work
initInput();

onInput(['arrowleft', 'swipeleft', 'dpadleft'], () => {
  // move left
});
import { initInput, onInput } from 'path/to/kontra.mjs';

// this function must be called first before input
// functions will work
initInput();

onInput(['arrowleft', 'swipeleft', 'dpadleft'], () => {
  // move left
});
import { initInput, onInput } from 'kontra';

// this function must be called first before input
// functions will work
initInput();

onInput(['arrowleft', 'swipeleft', 'dpadleft'], () => {
  // move left
});

Table of Contents

initInput(​[options])

Initialize input event listeners. This function must be called before using other input functions.

initInput Parameters

options Optional

Object. Input options.

options.pointer Optional

Object. Pointer options.

initInput Return value

Object. Object with pointer property which is the pointer object for the canvas.

offInput(​inputs[, options])

Unregister the callback function for an input. Takes a single input or an array of inputs.

let { offInput } = kontra;

offInput('left');
offInput(['enter', 'down', 'swipeleft']);
import { offInput } from 'path/to/kontra.mjs';

offInput('left');
offInput(['enter', 'down', 'swipeleft']);
import { offInput } from 'kontra';

offInput('left');
offInput(['enter', 'down', 'swipeleft']);

offInput Parameters

inputs

String or an Array of Strings. Inputs or inputs to unregister callback for.

options Optional

Object. Input options.

options.gamepad Optional

Object. offGamepad options.

options.key Optional

Object. offKey options.

onInput(​inputs, callback[, options])

Register a function to be called on an input event. Takes a single input or an array of inputs. See the keyboard, gamepad, gesture, and pointer docs for the lists of available input names.

let { initInput, onInput } = kontra;

initInput();

onInput('p', function(e) {
  // pause the game
});
onInput(['enter', 'down', 'south'], function(e) {
  // fire gun on enter key, mouse down, or gamepad south button
});
import { initInput, onInput } from 'path/to/kontra.mjs';

initInput();

onInput('p', function(e) {
  // pause the game
});
onInput(['enter', 'down', 'south'], function(e) {
  // fire gun on enter key, mouse down, or gamepad south button
});
import { initInput, onInput } from 'kontra';

initInput();

onInput('p', function(e) {
  // pause the game
});
onInput(['enter', 'down', 'south'], function(e) {
  // fire gun on enter key, mouse down, or gamepad south button
});

onInput Parameters

inputs

String or an Array of Strings. Inputs or inputs to register callback for.

callback

Function. The function to be called on the input event.

options Optional

Object. Input options.

options.gamepad Optional

Object. onGamepad options.

options.key Optional

Object. onKey options.