Why are some of my keys not working?

Why are some of my keys not working?


I’m using onkeydown and event.key to get keyboard input in my javascript calculator. There seems to be a problem in my switch statement, making use of the results:

window.onkeydown = function(e) {
  var key = e.key;
  console.log("event.key: " + key);
  var keyID;
  switch(key) {
    case "%":
      keyID = "percent";
    case "Backspace":
      keyID = "c";
    case ".":
      keyID = "dot";
      keyID = key;
  $("#" + keyID).click();
  console.log("keyID: " + keyID);

Pretty much everything comes out of the first console.log line (“event.key”), but many keys don’t make it through the second one (“keyID”). "-" comes through, but "+" and "/" don’t. Since the first log is coming through, the problem must not be with onkeydown or .key, but with my code, but I can’t see what it is. Help?

(Full pen: http://codepen.io/AbdiViklas/pen/GjdrZQ?editors=1011)


(I apologize if this answer is short. Not in a position to give further details)

The event on keydown uses codes instead of the character associated with the key.


Thanks! I was dismayed by the difference in those codes between top-row and numpad numbers, and the lack of distinction between “=” and “+”, and was ready to use a combo of keydown and keypress, but the .key property seems like a nice way around all that: http://www.w3schools.com/jsref/event_key_key.asp


Okay, solved my own question: I had a bunch of html elements with ids like “+”, “=”, etc. “-” just happens to be about the only one that was a valid ID name for jQuery purposes.