keycoder

A lightweight JavaScript library for interpreting event key and character codes across browsers.

Usage no npm install needed!

<script type="module">
  import keycoder from 'https://cdn.skypack.dev/keycoder';
</script>

README

Keycoder

npm version Build Status Coverage Status Dependency Status devDependency Status

A lightweight JavaScript library for interpreting event key and character codes across browsers. Keycoder implements UMD for CommonJS, AMD, and global based loading support.

Keycoder is released under the MIT license.

Modules

Keycoder

Classes

Key

Keycoder

Keycoder.key : object

An object containing references to all named keys.

Kind: static property of Keycoder
Properties

Name Type
BACKSPACE Key
TAB Key
ENTER Key
SHIFT Key
CONTROL Key
ALT Key
PAUSE Key
BREAK Key
CAPS_LOCK Key
ESCAPE Key
WINDOWS Key
COMMAND Key
OPTION Key
PRINT_SCREEN Key
NUM_LOCK Key
MAC_NUM_LOCK Key
SCROLL_LOCK Key
PAGE_UP Key
PAGE_DOWN Key
END Key
HOME Key
LEFT_ARROW Key
UP_ARROW Key
RIGHT_ARROW Key
DOWN_ARROW Key
INSERT Key
DELETE Key
NUMPAD_0 Key
NUMPAD_1 Key
NUMPAD_2 Key
NUMPAD_3 Key
NUMPAD_4 Key
NUMPAD_5 Key
NUMPAD_6 Key
NUMPAD_7 Key
NUMPAD_8 Key
NUMPAD_9 Key
NUMPAD_MULTIPLY Key
NUMPAD_PLUS Key
NUMPAD_MINUS Key
NUMPAD_DECIMAL Key
NUMPAD_DIVIDE Key
NUMPAD_MIDDLE Key

Keycoder.toCharacter(keyCode, shift) ⇒ string | null

Kind: static method of Keycoder
Returns: string | null - The character for the keycode and shift state. Null if the key is not a printable character.

Param Type Description
keyCode number An IE or Mozilla key code
shift boolean The shift key state. A value of true indicates it is pressed, false that it is not

Keycoder.charCodeToCharacter(charCode) ⇒ string | null

Kind: static method of Keycoder
Returns: string | null - Returns the character for the character code. Null if the key is not a printable character.

Param Type Description
charCode number An ASCII character code

Keycoder.eventToCharacter(event) ⇒ string | null

Kind: static method of Keycoder
Returns: string | null - - The character pressed in the key event. Null if the key pressed is not a printable character, or the event is not a key event.

Param Type Description
event number A keydown, keyup, or keypress event object

Keycoder.fromCharacter(character) ⇒ Key | null

Kind: static method of Keycoder
Returns: Key | null - A Key object. Null if no key is associated with the provided code.

Param Type
character string

Keycoder.fromKeyCode(keyCode) ⇒ Key | null

Kind: static method of Keycoder
Returns: Key | null - A Key object. Null if no key is associated with the provided code.

Param Type Description
keyCode number A IE or Mozilla key code

Keycoder.fromCharCode(charCode) ⇒ Key | null

Maps an ASCII character code to a Key object

Kind: static method of Keycoder
Returns: Key | null - A Key object. Null if no key is associated with the provided code.

Param Type Description
charCode number An ASCII character code

Keycoder.fromEvent(event) ⇒ Key | null

Maps a keypress, keydown, or keyup event object to a key

Kind: static method of Keycoder
Returns: Key | null - A Key object. Null if no key was pressed in the provided event.

Param Type Description
event object A keydown, keyup, or keypress event object

Keycoder.allKeys() ⇒ Array.<Key>

Kind: static method of Keycoder
Returns: Array.<Key> - An array of Key objects for all keys

Key

Kind: global class
Internal:
Properties

Name Type Description
names Array.<string> Names that the key is called. Ex. "BACKSPACE", "INSERT"
keyCode.ie number IE key code
keyCode.mozilla number Mozillia key code
character string | null Key character
charCode number | null ASCII character code
shift.character string | null Key shift character
shift.charCode number | null Shift ASCII character code

new Key()

A representation of a keyboard key. This class cannot be instantiated manually. All instances are generated by the Keycoder module.

key.isPrintableCharacter() ⇒ boolean

Kind: instance method of Key
Returns: boolean - If the key is a printable character

key.hasCharCode() ⇒ boolean

Kind: instance method of Key
Returns: boolean - If the key has a character code

key.hasDistinctShiftCharacter() ⇒ boolean

Kind: instance method of Key
Returns: boolean - If the key's character and shift character are different

key.equals(other) ⇒ boolean

Kind: instance method of Key
Returns: boolean - True if the key and the compared key/code are the same key

Param Type Description
other Key | number A Key object or key code