Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const Keys = new (class {
- constructor() {
- this.pressedKeys = [];
- this.keysToRemove = [];
- this.forbidden = [37, 38, 39, 40, 65, 68, 83, 87];
- }
- add(key) {
- if (!this.pressedKeys.includes(key)) {
- console.log(`Pressed ${String.fromCharCode(key)}`);
- this.pressedKeys.push(key);
- }
- }
- preRemove(key) {
- if (!this.keysToRemove.includes(key)) {
- this.keysToRemove.push(key);
- }
- }
- remove(key) {
- if (this.pressedKeys.includes(key)) {
- console.log(`Released ${String.fromCharCode(key)}`);
- this.pressedKeys.splice(this.pressedKeys.indexOf(key), 1);
- }
- }
- playerMovesUsingKeyboard() {
- for (const key of this.forbidden) {
- if (this.pressedKeys.includes(key)) {
- return true;
- }
- }
- return false;
- }
- })();
- //Usuwanie w wybranym momencie
- while (Keys.keysToRemove.length) {
- Keys.remove(Keys.keysToRemove[0]);
- Keys.keysToRemove.shift();
- }
- //Listenery
- document.addEventListener("keydown", (event) => {
- if (["INPUT", "TEXTAREA"].includes(event.target.tagName)) {
- return;
- }
- Keys.add(event.which);
- });
- document.addEventListener("keyup", (event) => {
- if (["INPUT", "TEXTAREA"].includes(event.target.tagName)) {
- return;
- }
- Keys.preRemove(event.which);
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement