Constructor
new KeyboardManager()
- Source
Examples
const { left } = DOM.KeyboardManagerInstance.keys;
if (DOM.KeyboardManagerInstance.isKeyPressed(left)) console.log("Left");
import { DOM } from "201flaviosilva-labs";
const { KEYS, KeyboardManager, KeyboardManagerInstance } = DOM;
setInterval(() => console.log(KeyboardManagerInstance.isKeyPressed(KEYS.a)), 100);
KeyboardManagerInstance.events.on("keyDown-left", () => console.log("down-left"));
KeyboardManagerInstance.events.on("keyUp-up", () => console.log(`up-${KEYS.up}`));
Members
(static) this.events
A event system to get on a key is pressed
- Source
DOM.KeyboardManagerInstance.events.on("keyDown-left", () => console.log("down -> left"));
DOM.KeyboardManagerInstance.events.on("keyUp-left", () => console.log("up -> left"));
(static) this.isPressed
All pressed keys will appear here
- Source
console.log(DOM.KeyboardManagerInstance.isPressed["left"]);
console.log(DOM.KeyboardManagerInstance.isPressed[DOM.KeyboardManagerInstance.getNameByKeyCode(37)]);
Methods
(static) KeyboardManager#getKeyCodeByName(name) → {number}
Returns the key code of the key by its name.
Parameters:
Name | Type | Description |
---|---|---|
name | string | The name of the key. |
- Source
Returns:
The key code of the key.
- Type:
- number
Example
console.log(DOM.KeyboardManagerInstance.getKeyCodeByName("left")); // 37
(static) KeyboardManager#getNameByKeyCode(keyCode) → {string}
Returns the name of the key by its keyCode.
Parameters:
Name | Type | Description |
---|---|---|
keyCode | number | The key code of the key. |
- Source
Returns:
The name of the key.
- Type:
- string
Example
const { left } = DOM.KeyboardManagerInstance.keys;
console.log(DOM.KeyboardManagerInstance.getNameByKeyCode(left)); // left
(static) KeyboardManager#isKeyPressed(key) → {boolean}
Returns true if the key is pressed.
Parameters:
Name | Type | Description |
---|---|---|
key | number | | The key code or name of the key. |
- Source
Returns:
True if the key is pressed.
- Type:
- boolean
Example
const { left } = DOM.KeyboardManagerInstance.keys;
DOM.KeyboardManagerInstance.isKeyPressed(left); // false
(static) KeyboardManager#isKeyPressedKeyCode(keyCode) → {boolean}
Returns true if the key is pressed by its key code.
Parameters:
Name | Type | Description |
---|---|---|
keyCode | number | The key code of the key. |
- Source
Returns:
True if the key is pressed.
- Type:
- boolean
Example
DOM.KeyboardManagerInstance.isKeyPressedKeyCode(37); // true
(static) KeyboardManager#isKeyPressedName(name) → {boolean}
Returns true if the key is pressed by its name.
Parameters:
Name | Type | Description |
---|---|---|
name | string | The name of the key. |
- Source
Returns:
True if the key is pressed.
- Type:
- boolean
Example
DOM.KeyboardManagerInstance.isKeyPressedName("left"); // true