mfcabrera
mfcabrera

Reputation: 781

After two keydown events sometimes the third one is not received

I am having a strange issue. I am using the following code to detect that a key is pressed. If more than one key is pressed then I perform certain action (it is not relevant). This works most of the time, but but certain key combinations (in particular when I press three of them) one of the events is not firing.

If I press 'a', 's', 'd' (at the same time) it works perfectly, 65, 83 and 86 get printed on the console. However if I press 's', 'd', 'e', only two of the codes get printed and the third only appear only when I release all the keys (not that there's no logging on the keyUp event). This only happens with certain key combinations.

I am using Chrome 59.0.3071.86 on Mac OS Sierra 10.12.5. I tried also on Safari and it has the same issue.

window.onkeydown = onKeyDown;
window.onkeyup = onKeyUp;
var lastEvent;

function onKeyDown(e){
  if (lastEvent && lastEvent.type == e.type && lastEvent.keyCode == e.keyCode) {
    return;
  }
  console.log(e.keyCode);
  // do stuff
  lastEvent = e;
}

function onKeyUp(e){

  if (lastEvent && lastEvent.type == e.type && lastEvent.keyCode == e.keyCode) {
    return;
  }
  // do stuff
  lastEvent = e;
}

Upvotes: 2

Views: 62

Answers (1)

Big Bad Waffle
Big Bad Waffle

Reputation: 404

I might be completely wrong here, but this sounds to me like a keyboard issue called ghosting. Modern gaming keyboards (for instance) overcome this issue by managing a buffer internally of keys that are pressed.

You can test if this is the case here: https://www.microsoft.com/appliedsciences/KeyboardGhostingDemo.mspx

Upvotes: 1

Related Questions