123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- class KeyboardInput {
- constructor() {
- this.keys = [];
- this.press = [];
- this.release = [];
- }
- attach() {
- for (var i = 0; i < 256; i++) {
- this.keys[i] = false;
- this.press[i] = false;
- this.release[i] = false;
- }
- window.addEventListener("keydown", (event) => {
- if (!this.keys[event.keyCode]) {
- this.press[event.keyCode] = true;
- }
- this.keys[event.keyCode] = true;
- });
- window.addEventListener("keyup", (event) => {
- if (this.keys[event.keyCode]) {
- this.release[event.keyCode] = true;
- }
- this.keys[event.keyCode] = false;
- });
- }
- isPressed(code) {
- var pressed = this.press[code] && this.keys[code];
- this.press[code] = false;
- return pressed;
- }
- isReleased(code) {
- var released = this.release[code] && !this.keys[code];
- this.release[code] = false;
- return released;
- }
- isDown(code) {
- return this.keys[code];
- }
- }
- window.Keys = {
- Left: 37,
- Up: 38,
- Right: 39,
- Down: 40,
- W: 87,
- A: 65,
- S: 83,
- D: 68,
- Space: 32,
- Enter: 13,
- Escape: 27
- };
|