🔰key event
🚧 under construction
keydown / keyup – keyboard key is pressed / released.
Despite its name, "keydown" fires not only when the key is physically pushed down. When a key is pressed and held, the event fires again every time the key repeats. Be carefull about this❗
Using key events to figure out what is being typed is problematic.
virtual keyboard on Android phones doesn’t fire key events.
input method editor (IME) may use multiple key strokes to create characters.
Elements like <input> and <textarea>, fire "input" events whenever the user changes their content. To get the actual content that was typed, it is best to directly read it from the focused field. 👉 Eloquent JS ⟩ Form Fields
The event.target of a key event is the element that has focus when the key is pressed.
replit ⟩ press "v"
// onkeydown
window.addEventListener("keydown", event => {
if (event.key === "v") {
document.body.style.background = "violet";
}
});
// onkeyup
window.addEventListener("keyup", event => {
if (event.key === "v") {
document.body.style.background = "";
}
});Last updated
Was this helpful?