dom-keyboard v1.0.1
DOM Keyboard
An in-browser representation of a keyboard built out of DOM nodes. It includes simple methods for interacting with key presses and making visual changes to the keyboard along side them. Open-source with zero dependencies.
This project is open-source! Feel free to submit pull requests or suggest changes, and check the github issues for ways to contribute!
Install
npm install dom-keyboard
Table of Contents
DOMKeyboardobjectKeyobject
Initializing
Call the DOMKeyboard constructor function with the width you want the keyboard to be, an optional id attribute, and an optional options object.
The width must be a percentage.
The id will be the DOM id of the keyboard.
More on the options object below.
The keyboard object has a node property that returns the DOM node for the keybaord.
const kb = new DOMKeyboard("100%", "my-keyboard", [options]);
document.body.appendChild(kb.node);options
A javascript object with the following optional properties:
reactToKeyPressboolean Default:trueDetermines if the keys of the DOMKeyboard are pressed when a user presses a key on their keybaord.
(Currently there is only one option, more are set to be added in future updates)
DOMKeyboard
Properties
keysAn array of all of theKeyobjects.nodeThe DOM<div>node holding the keyboard.
Methods
getKey(str)>>KeyTakes a string as an argument and usesKey.prototype.matchto return the first matchingKey.onKeyDown([...matches], callback)Specifies a callback to execute when a matching key is pressed on a user's keyboard. The callback receives the matchingKeyobject.matchescan be a string matching any property of aKeyobject, a series of such strings, or an array of such strings.kb.onKeyDown("t", (key) => console.log(key.code)); // When the "t" is pressed "KeyT" is logged kb.onKeyDown("t", "R", "KeyE", (key) => console.log(key.code)); // When the "t", "r", or "e" is pressed, the callback is executed kb.onKeyDown([["t", "R", "number"], (key) => console.log(key.code)); // When the "t", "r", or any number key is pressed, the callback is executedonKeyUp([...matches], callback)Specifies a callback to execute when a matching key is released on a user's keyboard. The callback receives the matchingKeyobject. SeeonKeyDownfor details.press(key, [time = 100], [calback])>>PromisePresses a key on the DOMKeyboard and releases it aftertimemilliseconds. The returnedPromiseis resolved after the key has been pressed and released in the DOMKeyboard.keyis a string that usesKey.prototype.matchto find all matching keys. A shift key will also be pressed ifkeymatches theshiftproperty of the matching key.callbackis an optional function that is called as each key is pressed. It is passed theKeyobject that is being pressed as an argument.timeandcallbackare all optional.callbackmust be the last argument if provided. NOTE: thepressmethod does not create a keypress event in the DOM.typeInto(node, text, [speed = 100], [variability = 0], [callback])>>PromiseEnters characters intonodeone character at a time while pressing the matching key on the DOMKeyboard.DOMKeyboard.prototype.pressis used to press the keys.nodeis a DOM node.textis a string that will be added one character at a time to the end ofnode's innerHTMLspeedis the time between key presses in millisecondsvariabilityis a time in milliseconds that thespeedwill randomly vary by to imitate inconsistent typing speed. If provided, a random number betweenspeed - variabilityandspeed + variabilitywill be chosen for each key press. Aspeedmust be specified in order to provide avariabilitycallbackis an optional function that is called as each key is pressed. It is passed theKeyobject that is being pressed as an argument.speed,variability, andcallbackare all optional.callbackmust be the last argument if provided. The returnedPromiseis resolved after the fulltexthas been typed into thenode
Key
Properties
characterThe main character for the key. A string.codeTheKeyboardEvent.keyvalue for the key. A StringnodeThe DOM<div>node holding the key.shiftThe character for if the key is pressed while shift is held. A StringsideThe side of the keyboard that the key is on. Either the string "Left" or "Right"typeThe type of the key'scharacter. A string. One of the following:- "letter"
- "number"
- "control"
- "special"
- "arrow"
Methods
down([time = 100])>>PromiseAdds thekeyboard-key-downCSS class to the key's node. The returnedPromiseis resolved aftertimemilliseconds.up([time = 100])>>PromiseRemoves thekeyboard-key-downCSS class to the key's node. The returnedPromiseis resolved aftertimemilliseconds.match(selected)>>booleanChecks whetherselectedmatches the key'scode,character,shift, ortypepropertyselectedcan be a string or array of strings.press([time = 100])>>PromiseCalls theKey.prototype.downmethod then delaystimemilliseconds before callingKey.prototype.upThe returnedPromiseis resolved after the key down and key up animaition have finished.style(cssStyle, newValue)Sets inline styles on the key's node using it's DOM Style Object. To unset any added styles, set the value tonull.const t = kb.getKey("t"); t.style("backgroundColor", "red") t.style("backgroundColor", null)