Whether the fact that a lot of features have been deprecated or are not part of the current standard for the KeyboardEvent object class. Was just wondering why whenever I press either shift or esc. Non of the keys show up on my console for Chrome or Mozilla.
document.addEventListener("keypress", (e) => {
console.log(e.keyCode);
console.log(e.key);
console.log(e.code);
console.log(e.shiftKey);
});
Browser Consoles used
https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent
keypress is only triggered when a printable character is pressed. For all keys, you need to work with keyup or keydown.
From MDN:
keypress
The keypress event is fired when a key that produces a character value is pressed down. Examples of keys that produce a character value are alphabetic, numeric, and punctuation keys. Examples of keys that don't produce a character value are modifier keys such as Alt, Shift, Ctrl, or Meta.
document.addEventListener("keydown", (e) => {
console.log(e.keyCode);
console.log(e.key);
console.log(e.code);
console.log(e.shiftKey);
});
<h1>Click into this area and then press ESC</h1>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With