add class for keyboard events
This commit is contained in:
parent
5057de5572
commit
0a95900c55
2 changed files with 47 additions and 0 deletions
|
|
@ -1,3 +1,4 @@
|
|||
export { backgroundLayout } from "./layouts";
|
||||
export { makeDragable, makeUnDragable } from "./dragAndDrop";
|
||||
export { getIntersection } from "./helpers";
|
||||
export { KeyBoardListener } from "./keyBoardEvents";
|
||||
|
|
|
|||
46
src/common/keyBoardEvents.js
Normal file
46
src/common/keyBoardEvents.js
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
export class KeyBoardListener {
|
||||
static _listenersKeyUp = {};
|
||||
static _listenersKeyDown = {};
|
||||
static ready = false;
|
||||
static _listenCount = 0;
|
||||
static init() {
|
||||
document.addEventListener("keydown", (event) => {
|
||||
for (const listener of Object.values(
|
||||
KeyBoardListener._listenersKeyDown,
|
||||
)) {
|
||||
listener(event);
|
||||
}
|
||||
});
|
||||
document.addEventListener("keyup", (event) => {
|
||||
for (const listener of Object.values(
|
||||
KeyBoardListener._listenersKeyUp,
|
||||
)) {
|
||||
listener(event);
|
||||
}
|
||||
});
|
||||
|
||||
KeyBoardListener.ready = true;
|
||||
}
|
||||
|
||||
/* return id used to remove the listener */
|
||||
static onKeyDown(callback) {
|
||||
const id = KeyBoardListener._listenCount++;
|
||||
KeyBoardListener._listenersKeyDown[id] = callback;
|
||||
return id;
|
||||
}
|
||||
|
||||
static offKeyDown(id) {
|
||||
delete KeyBoardListener._listenersKeyDown[id];
|
||||
}
|
||||
|
||||
/* return id used to remove the listener */
|
||||
static onKeyUp(callback) {
|
||||
const id = KeyBoardListener._listenCount++;
|
||||
KeyBoardListener._listenersKeyUp[id] = callback;
|
||||
return id;
|
||||
}
|
||||
|
||||
static offKeyUp(id) {
|
||||
delete KeyBoardListener._listenersKeyUp[id];
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue