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!
npm install dom-keyboard
-
DOMKeyboard
object -
Key
object
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);
A javascript object with the following optional properties:
reactToKeyPress
boolean
Default:true
Determines 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)
-
getKey(str)
>>Key
Takes a string as an argument and usesKey.prototype.match
to 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 matchingKey
object.
matches
can be a string matching any property of aKey
object, 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 executed
-
onKeyUp([...matches], callback)
Specifies a callback to execute when a matching key is released on a user's keyboard. The callback receives the matchingKey
object.
SeeonKeyDown
for details. -
press(key, [time = 100], [calback])
>>Promise
Presses a key on the DOMKeyboard and releases it aftertime
milliseconds.
The returnedPromise
is resolved after the key has been pressed and released in the DOMKeyboard.
key
is a string that usesKey.prototype.match
to find all matching keys.
A shift key will also be pressed ifkey
matches theshift
property of the matching key.
callback
is an optional function that is called as each key is pressed. It is passed theKey
object that is being pressed as an argument.
time
andcallback
are all optional.callback
must be the last argument if provided.
NOTE: thepress
method does not create a keypress event in the DOM. -
typeInto(node, text, [speed = 100], [variability = 0], [callback])
>>Promise
Enters characters intonode
one character at a time while pressing the matching key on the DOMKeyboard.DOMKeyboard.prototype.press
is used to press the keys.
node
is a DOM node.
text
is a string that will be added one character at a time to the end ofnode
's innerHTML
speed
is the time between key presses in milliseconds
variability
is a time in milliseconds that thespeed
will randomly vary by to imitate inconsistent typing speed. If provided, a random number betweenspeed - variability
andspeed + variability
will be chosen for each key press. Aspeed
must be specified in order to provide avariability
callback
is an optional function that is called as each key is pressed. It is passed theKey
object that is being pressed as an argument.
speed
,variability
, andcallback
are all optional.callback
must be the last argument if provided.
The returnedPromise
is resolved after the fulltext
has been typed into thenode
-
character
The main character for the key. A string. -
code
TheKeyboardEvent.key
value for the key. A String -
node
The DOM<div>
node holding the key. -
shift
The character for if the key is pressed while shift is held. A String -
side
The side of the keyboard that the key is on. Either the string "Left" or "Right" -
type
The type of the key'scharacter
. A string.
One of the following:- "letter"
- "number"
- "control"
- "special"
- "arrow"
-
down([time = 100])
>>Promise
Adds thekeyboard-key-down
CSS class to the key's node.
The returnedPromise
is resolved aftertime
milliseconds. -
up([time = 100])
>>Promise
Removes thekeyboard-key-down
CSS class to the key's node.
The returnedPromise
is resolved aftertime
milliseconds. -
match(selected)
>>boolean
Checks whetherselected
matches the key'scode
,character
,shift
, ortype
property
selected
can be a string or array of strings. -
press([time = 100])
>>Promise
Calls theKey.prototype.down
method then delaystime
milliseconds before callingKey.prototype.up
The returnedPromise
is 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)