Skip to content

Commit

Permalink
Update script.js
Browse files Browse the repository at this point in the history
  • Loading branch information
samolukadjo authored Aug 31, 2024
1 parent 74cbe4b commit 2605993
Showing 1 changed file with 62 additions and 1 deletion.
63 changes: 62 additions & 1 deletion script.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,67 @@ buttonEqual.addEventListener('click', function(){
errorSetting();
})

document.addEventListener("keydown", function(event) {
if (event.defaultPrevented) {
return; // Do nothing if the event was already processed
}

let val = null;
switch (event.key) {
case "0":
case "1":
case "2":
case "3":
case "4":
case "5":
case "6":
case "7":
case "8":
case "9":
val = event.key;
break;
case "/":
case "*":
case "+":
case "-":
case "(":
case ")":
case ".":
val = event.key;
break;
case "Enter":
val = "=";
break;
case "=":
val = "=";
break;
case "Backspace":
val = "Del";
break;
case "Escape":
val = "C";
break;
case "C":
val = "C";
break;
case "c":
val = "C";
break;
default:
return; // Do not act on other keys
}

console.log("Keypress detected: " + val);
if (val === "C") {
clearDisplay();
} else if (val === "=") {
evaluateDisplay();
} else {
appendDisplay(val);
}
event.preventDefault();
});

function errorSetting() {
counter++;
if (counter == 5) {
Expand Down Expand Up @@ -101,4 +162,4 @@ function checkForUpdates() {
}
}

setTimeout(checkForUpdates, 2000);
setTimeout(checkForUpdates, 2000);

0 comments on commit 2605993

Please sign in to comment.