Skip to main content

Class: Inputs<T>

A key and mouse binding manager for Voxelize.

Inputs allow you to bind keys and mouse buttons to functions and also gives an organized way to manage keyboard and mouse inputs using namespaces. Namespaces are used to separate groups of inputs. For example, you can have a namespace for the main menu and another namespace for the game. You can then bind keys and mouse buttons to functions for each namespace.

Another use of inputs is to bind keys and mouse buttons for some built-in functionality. As of now, the following requires inputs to be bound:

You can change the above bindings by calling Inputs.remap with the corresponding input identifiers, namely RigidControls.INPUT_IDENTIFIER and Perspectives.INPUT_IDENTIFIER.

Example​

// Create a new inputs manager.
const inputs = new VOXELIZE.Inputs();

// Bind the space bar to a function.
inputs.bind(" ", (event) => {
console.log("Space bar pressed!", event);
});

// Bind rigid controls to the inputs manager.
rigidControls.connect(inputs);

Type parameters​

NameTypeDescription
Textends string = anyThe list of input namespaces. For instance, T could be "menu" and "game".

Hierarchy​

  • EventEmitter

    ↳ Inputs

Constructors​

constructor​

• new Inputs<T>(): Inputs<T>

Construct a Voxelize inputs instance.

Type parameters​

NameType
Textends string = any

Returns​

Inputs<T>

Overrides​

EventEmitter.constructor

Properties​

namespace​

• namespace: "*" | T

The namespace that the Voxelize inputs is in. Use setNamespace to set the namespace to something else.

Methods​

bind​

▸ bind(key, callback, namespaces?, specifics?): () => void

Bind a keyboard key to a callback.

Parameters​

NameTypeDefault valueDescription
keystringundefinedThe key to listen for. This checks the event.key or the event.code property.
callback(event: KeyboardEvent) => boolean | voidundefinedThe callback to call when the key is pressed.
namespaces"*" | T | T[]"*"-
specificsInputSpecifics{}The specific options of the key to listen for.

Returns​

fn

A function to unbind the key.

▸ (): void

Returns​

void


click​

▸ click(type, callback, namespace?, occasion?): () => boolean

Add a mouse click event listener.

Parameters​

NameTypeDefault valueDescription
typeClickTypeundefinedThe type of click to listen for. Either "left", "middle" or "right".
callback(event: MouseEvent) => boolean | voidundefinedThe callback to call when the click is fired, passing the MouseEvent.
namespace"*" | T"*"The namespace to bind the click to. Defaults to "*", which means that the click will be fired regardless of the namespace.
occasionClickOccasion"mousedown"Whether to fire on press or on release. Defaults to "mousedown".

Returns​

fn

A function to unbind the click.

▸ (): boolean

Returns​

boolean


dispose​

▸ dispose(): void

Take the instance off the document for good: every key, click and scroll listener it installed is removed and every binding dropped.

The listeners close over this instance, and through its bindings over whatever the callbacks reach -- the controls, the world, the scene. An Inputs that is simply let go of keeps hearing every keystroke and keeps all of that alive with it. A page that mounts a new Inputs (a hot-reload remount, a world switch without a page load) must dispose the old one first.

Returns​

void


on​

▸ on(event, listener): Inputs<T>

Listen to an event emitted by the input instance. The following events are emitted:

  • namespace: Emitted when the namespace is changed.

Parameters​

NameTypeDescription
event"namespace"An event to listen on.
listener(namespace: string) => voidA listener to call when the event is emitted.

Returns​

Inputs<T>

The input instance for chaining.

Overrides​

EventEmitter.on


remap​

▸ remap(oldKey, newKey, specifics?): void

Remap a key to another key.

Parameters​

NameTypeDescription
oldKeystringThe old key to replace.
newKeystringThe new key to replace the old key with.
specificsObjectThe specifics of the keys to replace.
specifics.checkType?"key" | "code"-
specifics.identifier?string-
specifics.occasion?InputOccasion-

Returns​

void


reset​

▸ reset(): void

Reset all keyboard keys by unbinding all keys.

Returns​

void


scroll​

▸ scroll(up, down, namespace?): () => boolean

Add a scroll event listener.

Parameters​

NameTypeDefault valueDescription
up(delta?: number) => boolean | voidundefinedThe callback to call when the scroll wheel is scrolled up.
down(delta?: number) => boolean | voidundefinedThe callback to call when the scroll wheel is scrolled down.
namespace"*" | T"*"The namespace to bind the scroll to. Defaults to "*", which means that the scroll will be fired regardless of the namespace.

Returns​

fn

A function to unbind the scroll.

▸ (): boolean

Returns​

boolean


setNamespace​

▸ setNamespace(namespace): void

Set the namespace of the input instance. This emits a "namespace" event.

Parameters​

NameTypeDescription
namespaceTThe new namespace to set.

Returns​

void


swap​

▸ swap(keyA, keyB, specifics?): void

Swap two keys with each other.

Parameters​

NameTypeDescription
keyAstringThe first key to swap.
keyBstringThe second key to swap.
specificsObjectThe specifics of the keys to swap.
specifics.checkType?"key" | "code"-
specifics.identifier?string-
specifics.occasion?InputOccasion-

Returns​

void


unbind​

▸ unbind(key, specifics?): boolean

Unbind a keyboard key.

Parameters​

NameTypeDescription
keystringThe key to unbind.
specificsInputSpecificsThe specifics of the key to unbind.

Returns​

boolean

Whether or not if the unbinding was successful.