listen to key press javascript code example
Example 1: how to detect a keypress in javascript
window.addEventListener("keydown", (event) => {
console.log(event.key);
});
Example 2: adding event listener keypress event in javascript
let msg = document.getDocumentById('#message');
msg.addEventListener("keydown", (event) => {
// handle keydown
});
msg.addEventListener("keypress", (event) => {
// handle keypress
});
msg.addEventListener("keyup", (event) => {
// handle keyup
});