добавив ввод с клавиатуры "backspace" и "delete" в мой калькулятор javascript - PullRequest
0 голосов
/ 24 марта 2020

Я хочу добавить клавишу Backspace и удалить обнаружение клавиш с клавиатуры на моем JavaScript калькуляторе

Мой калькулятор уже обнаруживает физические клавиши, такие как цифры от 0 до 9 и клавишу "c" для очистки ввода и введите ключ

, но все еще не можете очистить ввод с помощью клавиш «backspace» и «delete», таких как «c», пожалуйста, помогите мне с этим

//define Variables
var calculator = document.querySelector("#display"),
  keys = document.querySelectorAll(".key"),
  value,
  first_number,
  second_number;

for (i = 0; i < keys.length; i++) {
  keys[i].addEventListener("click", clicked);
}

//define inputs from user
function clicked() {
  var key = this.innerText || this.textContent;
  keyPressed(key);
}

window.addEventListener("keypress", function(e) {
  e = e || window.event;
  keyPressed(e.key);
});

function keyPressed(key) {
  var val = display.value;

  if (isNaN(key)) {
    switch (key) {
      case "c":
      case "Delete":
        resetDisplay();
        value = "";
        break;
      case "±":
        if (val.startsWith("-")) {
          display.value = val.substr(1, val.length);
        } else {
          display.value = "-" + val;
        }
        break;
      case ".":
        if (val === "" || val === "-") {
          display.value += "0.";
        } else {
          display.value += ".";
        }
        break;
      case "%":
        display.value = val / 100;
        break;
      case "+":
        operation("+");
        break;
      case "-":
        operation("-");
        break;
      case "×":
      case "X":
      case "*":
        operation("×");
        break;
      case "÷":
      case "/":
        operation("÷");
        break;
      case "Enter":
      case "=":
        equals();
        break;
    }
  } else {
    if (val.length < 9) {
      display.value += key;
    }
  }
}
//define calculation
function equals() {
  if (first_number !== undefined && value) {
    second_number = parseFloat(display.value);
    switch (value) {
      case "+":
        display.value =
          Math.round((first_number + second_number) * 10000) / 10000;
        break;
      case "-":
        display.value =
          Math.round((first_number - second_number) * 10000) / 10000;
        break;
      case "×":
        display.value =
          Math.round(first_number * second_number * 10000) / 10000;
        break;
      case "÷":
        if (second_number === 0) {
          display.value = "ERROR            خـطا";
        } else {
          display.value =
            Math.round((first_number / second_number) * 10000) / 10000;
        }
        break;
    }
    value = "";
  }
}

function operation(type) {
  if (display.value === "") {
    display.value = 0;
  }
  if (value) {
    equals();
    first_number = parseFloat(display.value);
  } else {
    first_number = parseFloat(display.value);
  }
  value = type;
  display.value = "";
  display.placeholder = first_number + value;
}
//reset display
function resetDisplay() {
  display.value = "";
  display.placeholder = 0;
}
@import url("https://fonts.googleapis.com/css?family=Fira+Mono&display=swap");
* {
  margin: 0;
  padding: 0;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  background-color: #e9e9e9b9;
  font-family: "Fira Mono", monospace, sans-serif;
  font-size: 20px;
}

body {
  height: 100vh;
  width: 100vw;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  -webkit-box-pack: center;
  -ms-flex-pack: center;
  justify-content: center;
}

main {
  -webkit-box-shadow: -6px -6px 9px #fff, 6px 6px 9px rgba(0, 0, 0, 0.4);
  box-shadow: -6px -6px 9px #fff, 6px 6px 9px rgba(0, 0, 0, 0.4);
  padding: 2rem;
  border-radius: 30px;
}

input,
button {
  outline: none;
}

input::-webkit-inner-spin-button,
input::-webkit-outer-spin-button {
  -webkit-appearance: none;
}

input[type="number"] {
  -moz-appearance: textfield;
}

input {
  -webkit-box-shadow: inset -6px -6px 9px #fff, inset 6px 6px 9px rgba(0, 0, 0, 0.4);
  box-shadow: inset -6px -6px 9px #fff, inset 6px 6px 9px rgba(0, 0, 0, 0.4);
  width: 320px;
  height: 60px;
  padding: 16px 32px;
  text-align: right;
  border: none;
  border-radius: 30px;
  text-shadow: -1px -1px 2px #fff, 2px 2px 2px rgba(0, 0, 0, 0.4);
}

.keys {
  display: -ms-grid;
  display: grid;
  -ms-grid-columns: (1fr)[4];
  grid-template-columns: repeat(4, 1fr);
  grid-gap: 0.8rem;
  margin-top: 20px;
}

button {
  -webkit-box-shadow: -6px -6px 9px #fff, 6px 6px 9px rgba(0, 0, 0, 0.4);
  box-shadow: -6px -6px 9px #fff, 6px 6px 9px rgba(0, 0, 0, 0.4);
  width: 60px;
  height: 60px;
  border: none;
  border-radius: 30px;
  cursor: pointer;
  background: #e9e9e9;
}

button:hover,
button:active {
  background-color: #ccc;
}

button:active {
  -webkit-box-shadow: inset -6px -6px 9px #fff, inset 6px 6px 9px rgba(0, 0, 0, 0.4);
  box-shadow: inset -6px -6px 9px #fff, inset 6px 6px 9px rgba(0, 0, 0, 0.4);
}

.op__key,
.eq__key {
  color: #31456a;
  background: #dbdbdb;
}

.negate {
  font-size: 26px;
}

.create {
  margin: 0 auto;
  position: ;
  bottom: 25px;
}

.create>p {
  text-shadow: -1px -1px 2px #fff, 2px 2px 2px rgba(0, 0, 0, 0.4);
  font-size: 12px;
  color: 69;
}


/*# sourceMappingURL=styles.css.map */
<main>
  <input type="text" id="display" class="display" type="text" placeholder="0" />
  <div class="keys">
    <button class="op__key key" op="clear">c</button>
    <button class="op__key key negate" op="negate">±</button>
    <button class="op__key key" op="percent">%</button>
    <button class="op__key key" op="divide">/</button>
    <button class="num__key key">7</button>
    <button class="num__key key">8</button>
    <button class="num__key key">9</button>
    <button class="op__key key" op="multiply">X</button>
    <button class="num__key key">4</button>
    <button class="num__key key">5</button>
    <button class="num__key key">6</button>
    <button class="op__key key" op="subtract">-</button>
    <button class="num__key key">1</button>
    <button class="num__key key">2</button>
    <button class="num__key key">3</button>
    <button class="op__key key" op="add">+</button>
    <span></span>
    <button class="num__key  key">0</button>
    <button class="num__key key">.</button>
    <button class="eq__key key">=</button>
  </div>
</main>

https://playcode.io/523488

...