From ebf3197d5c9669f824d916b0bfaeebf4ef38b5d7 Mon Sep 17 00:00:00 2001 From: Anuj Sharma Date: Fri, 22 May 2026 20:55:51 +0530 Subject: [PATCH] Link the calculator with the keyboard --- web-app/js/projects.js | 97 +++++++++++++++++++++------ web-app/js/projects/calculator.js | 106 ++++++++++++++++++++---------- 2 files changed, 146 insertions(+), 57 deletions(-) diff --git a/web-app/js/projects.js b/web-app/js/projects.js index 24e291d..82d3e56 100644 --- a/web-app/js/projects.js +++ b/web-app/js/projects.js @@ -1323,29 +1323,29 @@ function getCalculatorHTML() {
0
- - - - + + + + - - - - + + + + - - - - + + + + - - - - + + + + - - - + + +
@@ -1421,7 +1421,7 @@ function getCalculatorHTML() { `; } - +let calculatorKeyboardAdded = false; function initCalculator() { const display = document.getElementById('calcDisplay'); let currentValue = '0'; @@ -1442,7 +1442,59 @@ function initCalculator() { updateDisplay(); }); }); - + if (!calculatorKeyboardAdded) { + document.addEventListener("keydown", (e) => { + const display = document.getElementById("calcDisplay"); + if (!display) return; + + const key = e.key; + + if ( + key === "Enter" || + key === " " || + key === "Backspace" || + key === "Escape" || + key === "=" || + key === "+" || + key === "-" || + key === "*" || + key === "/" || + key === "^" || + key === "." || + /^[0-9]$/.test(key) || + key.startsWith("Arrow") + ) { + e.preventDefault(); + } + + if (/^[0-9]$/.test(key)) { + handleNumber(key); + } + else if (key === ".") { + handleNumber("."); + } + else if (["+", "-", "*", "/"].includes(key)) { + handleAction(key); + } + else if (key === "^") { + handleAction("**"); + } + else if (key === "Enter" || key === "=") { + handleAction("="); + } + else if (key === "Backspace") { + handleAction("delete"); + } + else if (key === "Escape" || key.toLowerCase() === "c") { + handleAction("clear"); + } + + updateDisplay(); + }); + + calculatorKeyboardAdded = true; +} + function handleNumber(num) { if (currentValue === '0' || currentValue === 'Error') { currentValue = num; @@ -1494,9 +1546,12 @@ function initCalculator() { } function updateDisplay() { + const display = document.getElementById('calcDisplay'); + if (display) { display.textContent = currentValue; } } +} // ============================================ // FIBONACCI diff --git a/web-app/js/projects/calculator.js b/web-app/js/projects/calculator.js index efef28f..3def2f0 100644 --- a/web-app/js/projects/calculator.js +++ b/web-app/js/projects/calculator.js @@ -3,47 +3,47 @@ function getCalculatorHTML() {

🧮 Ultra Pro Calculator

-
+
0
- - - - + + + + - - - - + + + + - - - - + + + + - - - - + + + + - - - - + + + + - - - - + + + + - - - + + +
@@ -207,25 +207,59 @@ function initCalculator() { default: expression += action; } - update(); }); }); document.addEventListener("keydown", (e) => { - if (!isNaN(e.key) || e.key === ".") expression += e.key; + const key=e.key; + if(!document.getElementById("calcDisplay")) return; + if(key==="Enter" || + key==="Backspace" || + key==="Escape" || + key==="=" || + ["+","-","*","/","^",".","(",")"].includes(key) || + /^[0-9]$/.test(key) + ) { + e.preventDefault() + } + if(/^[0-9]$/.test(key)){ + expression+=key; + } + else if(key==="."){ + expression+="."; + } + else if(["+","-","*","/"].includes(key)){ + expression+=key; + } + else if(key===")" || key==="("){ + expression+=key; + } + else if(key==="^"){ + expression+="^"; + } + else if(key==="Enter" || key==="="){ + expression=safeEval(expression); + } + else if(key==="Backspace"){ + expression=expression.slice(0,-1); + } + else if(key==="Escape" || key.toLowerCase()==="c"){ + expression=""; + } + update(); + // if (!isNaN(e.key) || e.key === ".") expression += e.key; - if (["+", "-", "*", "/"].includes(e.key)) expression += e.key; + // if (["+", "-", "*", "/"].includes(e.key)) expression += e.key; - if (e.key === "^") expression += "^"; + // if (e.key === "^") expression += "^"; - if (e.key === "Enter") expression = safeEval(expression); + // if (e.key === "Enter") expression = safeEval(expression); - if (e.key === "Backspace") expression = expression.slice(0, -1); + // if (e.key === "Backspace") expression = expression.slice(0, -1); - update(); }); - + update(); }