Added the bricks (and improve the bouncing)
This commit is contained in:
24
Keyboard.js
Normal file
24
Keyboard.js
Normal file
@ -0,0 +1,24 @@
|
||||
class Keyboard {
|
||||
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];
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user