The keypress event is a deprecated KeyboardEvent for many printable keys (and Enter). Learn what it fired for, why MDN recommends keydown / beforeinput, how it differs from those APIs, and five try-it labs.
01
Kind
Instance event
02
Type
KeyboardEvent
03
When
Printable keys / Enter
04
Handler
onkeypress
05
Prefer
keydown / beforeinput
06
Status
Deprecated
Fundamentals
Introduction
Older tutorials often used keypress to react when the user typed a character. Today MDN marks it Deprecated and warns you to use beforeinput or keydown instead.
Historically, keypress fired for letters, digits, punctuation, symbols, and Enter—but not for modifier keys pressed alone (Alt, Shift, Ctrl, Meta, Esc, Option). That incomplete coverage is one reason modern code prefers keydown.
💡
Beginner tip
If you are writing new code, skip keypress. Use keydown for shortcuts and all-key detection, or beforeinput / input for text editing.
Concept
Understanding keypress
An instance event (legacy) that answered: “Did the user press a character-producing key (or Enter)?”
Deprecated on MDN — avoid in new projects.
Fires for many printable keys and Enter (with some Shift/Ctrl combos).
Usually does not fire for modifier keys alone (Alt, Shift, Ctrl, Meta, Esc, …).
KeyboardEvent — key, code, modifiers, repeat.
Handler — onkeypress or addEventListener("keypress", ...).
Replace withkeydown or beforeinput.
Foundation
📝 Syntax
Use the event name with addEventListener, or set the handler property:
A KeyboardEvent (inherits from UIEvent and Event).
Handy properties
Property
Meaning
key
Key value (e.g. "a", "Enter")
code
Physical key id (e.g. "KeyA", "Enter")
ctrlKey / shiftKey / altKey / metaKey
Modifier flags
repeat
true during auto-repeat while held
Compare
⚖️ keypress vs modern alternatives
API
Role today
keypress
Deprecated — printable/Enter only; avoid in new code
keydown
Preferred for shortcuts and all-key detection
keyup
Preferred for key-release logic
beforeinput
Preferred to inspect/cancel text edits early
input
Preferred to react after the value changed
Migration
🔄 Migrate to keydown
Most keypress listeners can become keydown listeners. Check event.key (or event.code) the same way, and remember keydown also fires for modifiers and arrows.
JavaScript
// Legacy (deprecated)
input.addEventListener("keypress", handler);
// Modern replacement
input.addEventListener("keydown", handler);
Cheat Sheet
⚡ Quick Reference
Goal
Code / note
Legacy listen
el.addEventListener("keypress", fn)
Handler property
el.onkeypress = fn
Modern shortcut
addEventListener("keydown", fn)
Modern text edit
beforeinput / input
MDN status
Deprecated
Snapshot
🔍 At a Glance
Four facts to remember about keypress.
Event type
KeyboardEvent
Same family as keydown
Means
printable / Enter
Not all keys
Status
deprecated
Avoid in new code
Replace
keydown
or beforeinput
Hands-On
Examples Gallery
Examples follow MDN Element: keypress event for learning legacy behavior—then show how to migrate to keydown.
📚 Legacy Patterns (Learning Only)
MDN-style listeners so you can recognize old code.
Example 1 — Log code on keypress (MDN)
Deprecated demo: append each KeyboardEvent.code for keys that still fire keypress.
Migration is often a one-line event-name change, then extra testing for modifiers and navigation keys.
Example 5 — Spot Legacy onkeypress Usage
A tiny audit helper for beginners reviewing old scripts.
JavaScript
const out = document.getElementById("out");
const input = document.querySelector("input");
// Simulate legacy assignment (do not copy this pattern)
input.onkeypress = () => {};
out.textContent = input.onkeypress
? "Legacy onkeypress is set — migrate to keydown."
: "No onkeypress handler found.";
keypress is marked Deprecated on MDN. Logos use the shared browser-image-sprite.png sprite from this project. Engines may still fire it for compatibility, but new code should use keydown or beforeinput.
Prefer keydown for shortcuts and all-key detection
Prefer beforeinput / input for text editing
Search legacy code for keypress / onkeypress and migrate
Document why a temporary legacy listener still exists
Test Escape, arrows, and modifiers after migration
❌ Don’t
Start new features on keypress
Expect Escape / Arrow / lone modifiers to fire keypress
Treat Deprecated as “fine forever”
Forget that MDN recommends keydown or beforeinput
Overwrite onkeypress if you must keep multiple legacy listeners
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about keypress
Deprecated character-key event—migrate to keydown.
5
Core concepts
📱01
Printable / Enter
not all keys
Trigger
📄02
KeyboardEvent
key · code
API
🚫03
Deprecated
avoid new use
Status
🔄04
Use keydown
modern replacement
Migrate
🎯05
beforeinput
for text edits
Alt
❓ Frequently Asked Questions
It fires when a letter, number, punctuation, or symbol key is pressed, or when Enter is pressed (including with Shift or Ctrl). Modifier keys alone (Alt, Shift, Ctrl, Meta, Esc, Option) usually do not fire keypress.
MDN marks Element keypress as Deprecated only — not Experimental and not Non-standard. Avoid it in new code; prefer keydown or beforeinput.
MDN recommends beforeinput or keydown. Use keydown for shortcuts and all-key detection; use beforeinput when you need to inspect text edits before they apply.
A KeyboardEvent (inherits from UIEvent and Event), same family as keydown and keyup.
keypress historically targeted character-producing keys. Modifier and many control keys do not fire it — another reason modern code prefers keydown.
Yes. It can reach Document and Window, like other keyboard events.
Did you know?
MDN’s explicit guidance is: since keypress is deprecated, you should use beforeinput or keydown instead—not keep building on keypress for compatibility alone.