I want to get keyup
and keydown
events with JavaScript, the problem is that at a long keyboard press those events are triggered automatically very often. Could I access the keys directly to get the physical events?
Keyup is fired once the key is released and the default action was performed. Keydown will repeat while key is not depressed.
So you can make an action the first time the keydown event is handled, and not doing it again until the keyup event was fired.
var action = true;
input.addEventListener('keydown', function () {
if (action === true) {
action = false;
// do your action here
}
});
input.addEventListener('keyup', function () {
action = true;
});