KeyboardEvent.repeat Guards Auto-repeat Keydown
Held keys re-fire keydown with event.repeat set to true, so guarding on repeat keeps one physical press equal to one action instead of dozens of toggles.
Code
let muted = false;
document.addEventListener('keydown', (event) => {
// Holding a key keeps firing keydown with event.repeat === true;
// only the first physical press reports repeat === false.
if (event.repeat) {
console.log('auto-repeat ignored for', event.key);
return;
}
if (event.key === 'm') {
muted = !muted;
console.log('mute toggled to', muted);
}
});Inside this example — KeyboardEvent.repeat Guards Auto-repeat Keydown
All the action happens on top-level bindings: muted.
What to watch for
- The whole dependency story is the language version itself.
- This is browser-side code —
documentwill not resolve in Node.
Putting it to use
Output arrives under these labels: auto-repeat ignored for, mute toggled to — when the file runs.
Core lines
let muted = false;
Interactive Tools for This Topic
DOM Event Propagation VisualizerVisualize DOM event capturing, targeting, and bubbling phases in real time. Toggle stopPropagation and stopImmediatePropagation to see how events traverse the tree.SRI Hash GeneratorGenerate Subresource Integrity (SRI) hashes from resource content using SHA-256, SHA-384, or SHA-512, and copy a ready-to-use script tag.
FAQ
- Does KeyboardEvent.repeat Guards Auto-repeat Keydown run in plain Node.js?
- No — it touches `document`, which exist only in the browser. Run it in a page (or the DevTools console) instead of a bare Node process.
Report a problem with this exampleOpens a pre-filled issue on GitHub.