Skip to content

Commit

Permalink
[code] created a toggleKeyPressListener to toggle the key press liste…
Browse files Browse the repository at this point in the history
…ner for keybindings
  • Loading branch information
weiseng18 committed Sep 2, 2020
1 parent f43696c commit 7fe8497
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion js/start_menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,5 +176,5 @@ StartMenu.prototype.closeMenu = function(which) {
document.body.removeChild(this.ele);

// add keypress listener
document.addEventListener("keypress", tools.keyPressListener.bind(tools));
tools.toggleKeyPressListener();
}
15 changes: 15 additions & 0 deletions js/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ function ToolWrapper(rows, columns) {

// currently, keybinds will be a single letter only
this.keybinds = ["Q", "W", "E", "R", "T", "Y", "A", "S", "D", "F", "G", "H"];
this.enabledKeyPress = false;

// binded key press listener
this.bindedKeyPressListener = this.keyPressListener.bind(this);
}

ToolWrapper.prototype.addTool = function(tool) {
Expand Down Expand Up @@ -75,6 +79,17 @@ ToolWrapper.prototype.generateHTML = function() {
get(this.wrapperID).appendChild(table);
}

ToolWrapper.prototype.toggleKeyPressListener = function() {
if (this.enabledKeyPress) {
document.removeEventListener("keypress", this.bindedKeyPressListener);
this.enabledKeyPress = false;
}
else {
document.addEventListener("keypress", this.bindedKeyPressListener);
this.enabledKeyPress = true;
}
}

ToolWrapper.prototype.keyPressListener = function(e) {
var key = e.which || e.keyCode;

Expand Down

0 comments on commit 7fe8497

Please sign in to comment.