How to prevent Jquery keydown/keyup events returning multiple events when a key is mapped to multiple characters?

StackOverflow https://stackoverflow.com/questions/1726403

  •  19-09-2019
  •  | 
  •  

Question

$(document).ready(function() {
    $(document).keyup(function(e) {
        alert('This key: ' + e.keyCode);
    });
});

If an individual key is mapped to multiple characters, then the keyup event will execute multiple times for one key press.

Does anyone know how to identify just the physical act of pressing the key in this instance?

Cheers

Was it helpful?

Solution

Using keyup, keys like ctrl will not trigger an event. keydown do though.

Using the codes below, I can capture almost all physical keys pressed down (I think).

Script:

$(document).ready(function(){
    $(document).keydown(function(e){
        $("#key").html($("#key").html() + " " + e.keyCode)
    });
});

Html:

<div id="key"></div>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top