I want to intercept the event "keypress" with javascript through the following code:
document.addEventListener("keypress", (e) => {
if (e.key === "a") {
console.log("You pressed the key a");
}
}
The code actually works. The problem is that even though I press the a letter once, the string "You press the key a" is printed in my console 40 times. It should be the behaviour of keydown, not keypress.
As documented by the Developer Mozilla documentation, keypress feature is no longer supported.
With this little context I cannot find the problem, you could start by changing it with the 'keydown' event and see if it works.