Moved classes his own files

This commit is contained in:
2021-11-12 18:44:25 +01:00
parent c13c752716
commit 135f7d8a41
4 changed files with 121 additions and 0 deletions

24
Key.js Normal file
View File

@ -0,0 +1,24 @@
class Key {
constructor(onKeydown) {
this._pressed = {};
this.cb_onKeydown = onKeydown;
window.addEventListener('keydown', e => this.onKeydown(e));
window.addEventListener('keyup', e => this.onKeyup(e));
}
setKeydown(fn) {
this.cb_onKeydown = fn;
}
isDown(keyCode) {
return this._pressed[keyCode];
}
onKeydown(event) {
this._pressed[event.code] = true;
if (this.cb_onKeydown) this.cb_onKeydown(event);
}
onKeyup(event) {
delete this._pressed[event.code];
}
}