@jworkshop/keyboard
v0.0.5
Published
An instance class which hooks into keyup and keydown, and keeps track of all the key pressed.
Downloads
1
Readme
keyboard
An instance class which hooks into keyup and keydown, and keeps track of all the key pressed.
install
Usage
import Keyboard, { Keys } from "@jworkshop/keyboard";
/* Get the container for the keyboard. */
let container = document.getElementById("container");
/* Create a keyboard instance, with the element as its container.
* This is to allow the keyboard to monitor all key events from the container. */
let keyboard = new Keyboard(container);
/** Append the keyboard to the a DOM element and event handlers to it. */
keyboard.attach(container);
/** Detach the keyboard from DOM element and event handlers from it. */
keyboard.detach();
/** Toggle value for keyboard prevent default on all events. */
keyboard.setPreventDefault(preventDefault);
/** Toggle value for keyboard skipping further key down events. */
keyboard.setPreventHoldDownEvent(preventHoldDownEvent);
let keyDownHandler = event => { ... };
/** Bind an event handler to the key down event. */
keyboard.onKeyDown(keyDownHandler);
/** Unbind an event handler to the key down event. */
keyboard.removeKeyDown(keyDownHandler);
/** Unbind all event handlers from the key down event. */
keyboard.clearKeyDown();
let keyUpHandler = event => { ... };
/** Bind an event handler to the key up event. */
keyboard.onKeyUp(keyUpHandler);
/** Unbind an event handler to the key up event. */
keyboard.removeKeyUp(keyUpHandler);
/** Unbind all event handlers from the key up event. */
keyboard.clearKeyUp();
/** Checks if a given keyCode has been pressed. */
keyboard.hasKeyPressed(keyCode);
/** You can use key constants as keyCodes e.g.: */
keyboard.hasKeyPressed(Keys.ENTER);
/** Get the last keyCode that has been pressed. */
keyboard.getLastKeyPressed();