Kaybee 🐝
A very tiny keyboard input library for games.
- Built on the KeyboardEvent API.
- Zero dependencies.
- < 0.4kb compressed.
Getting Started
via package manager:
yarn add kaybee or npm i -S kaybee
import { start } from "kaybee";
via CDN:
import { start } from "https://jspm.dev/kaybee/dist/kaybee.js";
Usage
const kb = start({
target: window,
renameKeys: true,
enableRepeat: false,
onKeyDown: ({ key, code, repeat }) => {},
onKeyUp: ({ key, code }) => {},
});
if (kb.getKey("f")) {
}
if (kb.getCode("KeyW")) {
}
kb.stop();
If renameKeys is true, then key names are transformed to more useful values using three simple rules:
- All key names become lowercase.
"A" -> "a"
- Arrow keys are unprefixed.
"ArrowLeft" -> "left"
- And the space key is given a real value.
" " -> "space"
Only the key name is changed by this option. The code name is unaffected.
This is recommended because the names given by the browser can be less than ideal for games. For example, "a" and "A" are given as two different key names.
See MDN's key and code pages for more info on names in the KeyboardEvent API.