简体   繁体   English

JavaScript keydown 事件 e.shiftKey + e.keyCode 8 星号

[英]JavaScript keydown event e.shiftKey + e.keyCode 8 asterisk

I'm working on a project for school and I'm writing a calculator.我正在为学校做一个项目,我正在写一个计算器。
I got to the point where I'm programming the keydown event, the numbers aren't causing any problems, but I got stuck at the %, * and +.我到了编写keydown事件的地步,这些数字没有引起任何问题,但我被困在了%、*和+上。

The output I receive in the console.log is correct - I'm getting the asterisk * each time I press shift + 8, not when I press shift or 8 individually.我在 console.log 中收到的 output 是正确的 - 每次按 shift + 8 时都会得到星号 *,而不是单独按 shift 或 8 时。 But what I'm getting in the #output.innerHTML is " *8 ", since I also have an event listener for number 8, as seen above.但是我在#output.innerHTML 中得到的是“*8”,因为我也有一个8 号的事件监听器,如上所示。

function forDisplay(value) {
      document.getElementById("output").innerHTML += value;
 
    }

document.addEventListener('keydown', (event)=>{
 if(event.keyCode === 56 || event.keyCode === 104){
        document.getElementById("output").innerHTML += 8
    }

 if(event.shiftKey && event.keyCode === 56){    
        console.log('*')
        forDisplay('*')
    }

})

Is there any way I can make the code work in the output too, since it's working correctly in the console.log?有什么方法可以使代码在 output 中工作,因为它在 console.log 中正常工作? Thanks!谢谢!

I see your problem - the first if is firing, and so is the second if when you press Shift and 8. Simply check for the Shift and 8 first, then add an else if statement, in case the condition is not true.我看到了您的问题 - 第一个if正在触发,当您按下 Shift 和 8 时,第二个if也是如此。只需先检查 Shift 和 8,然后添加else if语句,以防条件不成立。

 document.addEventListener('keydown', (event) => { if (event.shiftKey && event.keyCode === 56) { console.log('*') document.getElementById("output").innerHTML += "*"; } else if (event.keyCode === 56 || event.keyCode === 104) { document.getElementById("output").innerHTML += 8 } })
 <div id="output"></div>

Also, here's my attempt at a calculator, complete with regular numbers, addition, subtraction, multiplication, division, exponent, Num Pad, designated symbols for multiplication and division, prevention for operands next to each other (things like 132+*837 ), backspace, clearing options, and evaluation for Enter key, equal to, or Evaluate button:另外,这是我对计算器的尝试,包括常规数字、加法、减法、乘法、除法、指数、数字键盘、乘法和除法的指定符号、防止彼此相邻的操作数(例如132+*837 ), Enter 键、等于或Evaluate按钮的退格、清除选项和评估:

 var outputEl = document.getElementById("output") function CheckLastChar() { if (["×", "+", "-", "÷", "^"].includes(outputEl.innerText.slice(-1))) { outputEl.innerText = outputEl.innerText.slice(0, -1) } } document.addEventListener('keydown', (event) => { key = String.fromCharCode(event.keyCode); if (event.shiftKey) { if (key == "8") { CheckLastChar(); outputEl.innerText += "×"; } else if (key == "6") { CheckLastChar(); outputEl.innerText += "^"; } else if (event.keyCode == 187) { CheckLastChar(); outputEl.innerText += "+"; } } else { if ([...Array(10).keys()].map(el => el.toString()).includes(key)) { outputEl.innerText += key; } else if (event.keyCode == 106) { CheckLastChar(); outputEl.innerText += "×"; } else if (event.keyCode == 107) { CheckLastChar(); outputEl.innerText += "+"; } else if (event.keyCode == 189 || event.keyCode == 109) { CheckLastChar(); outputEl.innerText += "-"; } else if (event.keyCode == 191 || event.keyCode == 111) { CheckLastChar(); outputEl.innerText += "÷"; } else if (event.keyCode == 8) { outputEl.innerText = outputEl.innerText.slice(0, -1); } else if (event.keyCode == 13) { document.querySelectorAll("button")[0].click(); } else if (event.keyCode == 187) { document.querySelectorAll("button")[0].click(); } } }) document.querySelectorAll("button")[0].addEventListener("click", () => { stringToEvaluate = document.getElementById("output").innerText.replace("×", "*").replace("÷", "/").replace("^", "**"); console.clear(); try { console.log(eval(stringToEvaluate)); } catch { console.log("Unexpected error or invalid expression."); } }) document.querySelectorAll("button")[1].addEventListener("click", () => { outputEl.innerText = ""; })
 <div id="output"></div> <button>Evaluate!</button> <button>Clear!</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM