From 3c3cebfaad8853c6c7ba91dd41bcf54bb9dbdd8a Mon Sep 17 00:00:00 2001 From: delta student <04.sainiKhushi@gmail.com> Date: Sat, 25 Oct 2025 17:39:17 +0530 Subject: [PATCH] Fix keyboard handler: Escape clears input, remove parentheses input --- Projects/calculator-js/script.js | 46 +++++++++++++++++++------------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/Projects/calculator-js/script.js b/Projects/calculator-js/script.js index 96f5433c..360ceaf1 100644 --- a/Projects/calculator-js/script.js +++ b/Projects/calculator-js/script.js @@ -56,22 +56,32 @@ buttons.forEach(btn => { }); }); -// Keyboard support -window.addEventListener('keydown', (e) => { - if ((e.key >= '0' && e.key <= '9') || '+-*/().%'.includes(e.key)) { - currentExpression += e.key; - updateDisplay(); - resultDisplay.value = ''; - } else if (e.key === 'Enter') { - const sanitized = sanitizeExpression(currentExpression); - resultDisplay.value = calculateExpression(sanitized); - } else if (e.key === 'Backspace') { - currentExpression = currentExpression.slice(0, -1); - updateDisplay(); - resultDisplay.value = ''; - } else if (e.key.toLowerCase() === 'c') { - currentExpression = ''; - updateDisplay(); - resultDisplay.value = ''; - } +document.addEventListener('keydown', (e) => { + // Only digits, operators, and decimal + if ("0123456789+-*/.%".includes(e.key)) { + currentExpression += e.key; + updateDisplay(); + resultDisplay.value = ''; + } + // Evaluate on Enter + else if (e.key === 'Enter') { + e.preventDefault(); // Prevent form submission + const sanitized = sanitizeExpression(currentExpression); + resultDisplay.value = calculateExpression(sanitized); + } + // Delete last character + else if (e.key === 'Backspace') { + e.preventDefault(); // Prevent browser back navigation + currentExpression = currentExpression.slice(0, -1); + updateDisplay(); + resultDisplay.value = ''; + } + // Clear using Escape key + else if (e.key === 'Escape') { + e.preventDefault(); + currentExpression = ''; + updateDisplay(); + resultDisplay.value = ''; + } }); +