keydown event multiple key hold odd behavior

时间:2018-04-20 21:21:35

标签: javascript google-chrome ecmascript-6

I have been running the following code using es6 babel on chrome:

window.hold = [];
window.addEventListener('keydown', (e) => {
  if (!window.hold.includes(e.key)) {
    setTimeout(() => {
      console.log("> - " + e.key);
      window.hold.push(e.key);
    }, 0);
  } else { e.preventDefault(); }
});

window.addEventListener('keyup', (e) => {
  setTimeout(() => {
    console.log("< - " + e.key);
    window.hold.splice(window.hold.indexOf(e.key), 1);
    e.preventDefault();
  }, 0);
});

If I hold ASDZ on the keyboard, it doesn't log the Z, but if I hold ASDK, it logs everything as it should, does anyone know why this is happening?

0 个答案:

没有答案