About
Key-definitions module is made by one purpose: to have key definitions in one place. Key definitions are followed by MDN documentation and as recommended a web developers should check event.key
on keyup
event to detect pressed key (not a keydown
event nor keyCode
property). The reason for this read more in documentation.
Because there are numerous keyboard layouts this library is not 100% bulletproof.
Defined layouts follow QWERTY keyboards.
Usage
Before
const onKeyUpHandler = (event) => {
if (event.keyCode === 65) {
}
if (event.key === "ArrowLeft") {
}
if (event.key === " ") {
}
if (event.key === "A" || event.key === "B" || event.key === "C") {
}
};
useEffect(() => {
document.addEventListener("keyup", onKeyUpHandler);
return () => {
document.removeEventListener("keyup", onKeyUpHandler);
};
}, []);
Now
With new approach you can easily:
- forward event instance to compare function
- read a code easily by not memorizing what ASCII number means
import { UpperCase, ARROW_LEFT, SPACE } from "key-definitions";
const onKeyUpHandler = (event) => {
if (compare(event, ARROW_LEFT)) {
}
if (event.key === ARROW_LEFT.key) {
}
if (compare(event, [UpperCase.A, UpperCase.B, UpperCase.C])) {
}
};
useEffect(() => {
document.addEventListener("keyup", onKeyUpHandler);
return () => {
document.removeEventListener("keyup", onKeyUpHandler);
};
}, []);
Install
npm install key-definitions
or
yarn add key-definitions
Interfaces
Each key inherits KeyInterface
that has following structure:
interface KeyInterface {
keyCode: number | number[];
keyCodeDefinitions?: KeyCodeSupportOS[];
hex?: hexNumber | hexNumber[];
code: string;
key: string;
isAltKey?: boolean;
isMetaKey?: boolean;
isShiftKey?: boolean;
isCtrlKey?: boolean;
}
Functions
Name | Description | Return value |
---|
isCharacter | Detect if pressed value is a character a-z and A-Z. | boolean |
compare | compare two values; compares key property and code in case of SHIFT, ALT and CONTROL keyboard events | boolean |
compare (value: KeyboardEvent, equalToValue: KeyInterface)
Compare event with key definitions from library.
import { SPACE } from "key-definitions";
const onKeyUp = (e: KeyboardEvent) => {
compare(event, SPACE);
};
compare (value: KeyboardEvent, equalToValue: KeyInterface[])
Compare does event is one of the several key definitions from library.
import { SPACE, TAB } from "key-definitions";
const onKeyUp = (e: KeyboardEvent) => {
compare(e.key, [SPACE, TAB]);
};
isCharacter (x: KeyboardEvent)
Function will check key
, code
or keyCode
values of KeyboardEvent.
const onKeyUp = (e: KeyboardEvent) => {
isCharacter(e);
};
document.addEventListener("keyup", onKeyUp);
isCharacter(x: string) => boolean
isCharacter("A");
isCharacter("a");
isCharacter("?");
isCharacter("č");
isCharacter("1");
isCharacter("KeyA");
isCharacter("KeyZ");
isCharacter("Enter");
isCharacter(x: number)
Even keyCode
is deprecated it's still in usage by some developers. Forward keyCode
value from keyup
event.
isCharacter(64);
isCharacter(90);
isCharacter(191);
isCharacter(49);
Supported table
Language layouts
Language | Layout | FileName |
---|
English | US | - |
Croatian | CRO | Layout_CRO |
German | DE | Layout_DE |
By default US layout is set, for different layout you need to import it independently.
import { LowerCase }, Layout_CRO, Layout_DE from "key-definitions";
console.log(LowerCase.A.key);
console.log(Layout_CRO.LowerCase.A.key);
console.log(Layout_CRO.LowerCase.Č.key);
console.log(LowerCase.Č.key);
console.log(Layout_DE.LowerCase.Ü.key);
console.log(Layout_DE.LowerCase.UMLAUT_U.key);
Sections
Alpha
Includes lower and upper case a-zA-Z
.
Key | Layout |
---|
A-Z | all |
Č | cro |
Ć | cro |
Ž | cro |
Đ | cro |
Š | cro |
Ü (or UMLAUT_U) | de |
Ö (or UMLAUT_O) | de |
Ä (or UMLAUT_A) | de |
ẞ (or UMLAUT_S) | de |
import { LowerCase, UpperCase } from "key-definitions";
console.log(LowerCase.A.key);
console.log(UpperCase.A.key);
Digits
Includes all numerical keys 0-9
in alphanumeric keyboard section.
Key | Name |
---|
0 | ZERO |
1 | ONE |
2 | TWO |
3 | THREE |
4 | FOUR |
5 | FIVE |
6 | SIX |
7 | SEVEN |
8 | EIGHT |
9 | NINE |
import { ZERO, SEVEN } from "key-definitions";
console.log(ZERO.key);
console.log(SEVEN.key);
Numpad
Includes all keys in numpad section - digits and general (functional keys such as enter, num lock). Example: 0-9, NumLock, Enter
; We have two sections General
(functional keys) and Digits
(0-9 numbers).
Key | Name |
---|
0-9 | ZERO-NINE |
= (equal sign) | EQUAL |
enter | ENTER |
arrow up | ARROW_UP |
arrow down | ARROW_DOWN |
arrow right | ARROW_RIGHT |
arrow left | ARROW_LEFT |
* | MULTIPLY |
+ | ADD |
- | SUBSTRACT |
, | COMMA |
/ | DIVIDE |
. | DECIMAL |
insert | INSERT |
end | END |
page down | PAGE_DOWN |
home | HOME |
page up | PAGE_UP |
delete | DELETE |
import { Numpad } from "key-definitions";
console.log(Numpad.ARROW_DOWN.key);
console.log(Numpad.ZERO.key);
Functions
Includes all keys from F1-F12
in function section.
import { F1 } from "key-definitions";
console.log(F1.key);
Generics
Includes functional keys in alphanumeric keyboard section. Example: AltLeft, Shift, Enter, Tab, CapsLock
.
Key | Name |
---|
tab | TAB |
enter | ENTER |
shift left | SHIFT_LEFT |
shift right | SHIFT RIGHT |
ctrl left | CTRL_LEFT |
ctrl right | CTRL_RIGHT |
alt left | ALT_LEFT |
alt right | ALT_RIGHT |
caps lock | CAPS_LOCK |
escape | ESC |
space | SPACE |
page up | PAGE_UP |
page down | PAGE_DOWN |
end | END |
home | HOME |
arrow left | ARROW_LEFT |
arrow up | ARROW_UP |
arrow right | ARROW_RIGHT |
arrow down | ARROW_DOWN |
print screen | PRINT_SCREEN |
insert | INSERT |
delete | DELETE |
num lock | NUM_LOCK |
scroll lock | SCROLL_LOCK |
help | HELP |
pause | PAUSE |
import { ALT_LEFT, ALT_RIGHT } from "key-definitions";
console.log(ALT_LEFT.key);
console.log(ALT_LEFT.code);
console.log(ALT_RIGHT.key);
console.log(ALT_RIGHT.code);
Specials
Includes special keys like question marks )!?.$&%/#"\*)
as part of alphanumeric keyboard section.
Key | Name |
---|
! | EXCLAMATION_MARK |
" | QUOTATION_MARK |
# | HASH |
$ | DOLLAR_SIGN |
% | PERCENT |
& | AMPERSAND |
' | APOSTROPHE |
( | OPEN_PARENTHESIS |
) | CLOSE_PARENTHESIS |
* | ASTERISK |
+ | PLUS |
, | COMMA |
- | DASH |
. | DOT |
/ | SLASH |
: | COLON |
; | SEMICOLON |
< | LESS_THAN_BRACKET |
= | EQUAL |
> | GREATER_THAN_BRACKET |
? | QUESTION_MARK |
@ | AMPERSAT |
[ | OPEN_BRACKET |
\ | BACKSLASH |
] | CLOSE_BRACKET |
^ | CARET |
_ | UNDERSCORE |
` | BACKQUOTE |
{ | OPEN_BRACE |
| | VERTICAL_BAR |
} | CLOSE_BRACE |
~ | TILDA |
import { DOLLAR_SIGN } from "key-definitions";
console.log(DOLLAR_SIGN.key);
Other resources:
https://keycode.info/for/!
https://www.computerhope.com/jargon/s/specchar.htm
https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/code
https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode