Skip to content

6a. Input ‐ handlers

CookieShade edited this page Aug 7, 2016 · 5 revisions

Input handling

Becuase the old DOM is terrible, and the new DOM is terribly supported, BETA.js provides a different way to handle input.

Buttons are referred to by their names, as listed here.

BETA.initInput()

Starts up the input handling system. The rest of the input functions won't work without calling this first.

BETA.initInput(); // Well done!

BETA.isButtonDown(button)

Returns whether a button is currently held down.

BETA.initInput();

var isDown = BETA.isButtonDown("Mouse1"); // true or false

BETA.onButtonDown(button, callback)

Calls a function whenever a given button is pressed. Ignores repeat button-press events fired when a key is held down.

The callback is passed the event object (MouseEvent or KeyboardEvent) as the first argument.

BETA.initInput();

BETA.onButtonDown("Mouse1", function ()
{
    var isDown = BETA.isButtonDown("Mouse1"); // true
});

BETA.onButtonUp(button, callback)

Calls a function whenever a given button is released.

The callback is passed the event object (MouseEvent or KeyboardEvent) as the first argument.

BETA.initInput();

BETA.onButtonUp("Mouse1", function ()
{
    var isDown = BETA.isButtonDown("Mouse1"); // false
});

BETA.getMousePos(renderer)

Gets the current mouse position, relative to a canvas renderers top-left corner, in pixels.

Note: May not work correctly if the underlying element has had its CSS attributes (e.g. border) manually modified.

BETA.initInput();
var renderer = BETA.getRenderer("myCanvas");

BETA.onButtonDown("Mouse1", function ()
{
    var mouse = BETA.getMousePos(renderer); // Vector, e.g. {x: 250, y: 200}
});