JavaScript Element keypress Event

Beginner
⏱️ 11 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Deprecated
Instance event

What You’ll Learn

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

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.

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, …).
  • KeyboardEventkey, code, modifiers, repeat.
  • Handleronkeypress or addEventListener("keypress", ...).
  • Replace with keydown or beforeinput.

📝 Syntax

Use the event name with addEventListener, or set the handler property:

JavaScript
addEventListener("keypress", (event) => { });

onkeypress = (event) => { };

Event type

A KeyboardEvent (inherits from UIEvent and Event).

Handy properties

PropertyMeaning
keyKey value (e.g. "a", "Enter")
codePhysical key id (e.g. "KeyA", "Enter")
ctrlKey / shiftKey / altKey / metaKeyModifier flags
repeattrue during auto-repeat while held

🔄 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);

⚡ Quick Reference

GoalCode / note
Legacy listenel.addEventListener("keypress", fn)
Handler propertyel.onkeypress = fn
Modern shortcutaddEventListener("keydown", fn)
Modern text editbeforeinput / input
MDN statusDeprecated

🔍 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

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.

JavaScript
const log = document.getElementById("log");
const input = document.querySelector("input");

input.addEventListener("keypress", logKey);

function logKey(e) {
  log.textContent += " " + e.code;
}
Try It Yourself

How It Works

Try Escape or Arrow keys too—many will not appear, because keypress often skips them.

Example 2 — onkeypress Property (MDN)

Same legacy listener using the handler property.

JavaScript
const input = document.querySelector("input");
const log = document.getElementById("log");

function logKey(e) {
  log.textContent += " " + e.code;
}

input.onkeypress = logKey;
Try It Yourself

How It Works

Prefer addEventListener even in legacy maintenance—and migrate away from keypress when you can.

📈 Coverage Gaps & Migration

See which keys fire, then replace with keydown.

Example 3 — Which Keys Fire keypress?

Compare a printable key, Enter, and Escape side by side.

JavaScript
const field = document.getElementById("field");
const out = document.getElementById("out");

field.addEventListener("keypress", (event) => {
  out.textContent = "keypress fired: key=" + event.key + " code=" + event.code;
});

field.addEventListener("keydown", (event) => {
  if (event.key === "Escape" || event.key.startsWith("Arrow")) {
    out.textContent =
      "keydown only (no keypress expected): key=" + event.key;
  }
});
Try It Yourself

How It Works

This gap is why Escape-to-close and arrow navigation should use keydown, not keypress.

Example 4 — Migrate the Same Handler to keydown

Keep the logging function; only change the event name.

JavaScript
const input = document.querySelector("input");
const out = document.getElementById("out");

function logKey(e) {
  out.textContent = "Modern keydown: " + e.key + " / " + e.code;
}

// Prefer this over keypress
input.addEventListener("keydown", logKey);
Try It Yourself

How It Works

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.";
Try It Yourself

How It Works

In real projects, search the codebase for keypress / onkeypress and replace them deliberately.

🚀 When You Still See keypress

  • Reading older Stack Overflow answers and textbooks.
  • Maintaining legacy form scripts that filter typed characters.
  • Understanding why Escape/arrow shortcuts were “broken” in old demos.
  • Planning a migration to keydown / beforeinput.
  • Interview questions about deprecated DOM events.

🔧 How It Worked (Legacy)

1

Focus a field

The focused element receives keyboard events.

Target
2

keydown first

Modern engines still fire keydown for the press.

Down
3

Maybe keypress

Only some keys historically produced keypress.

Legacy
4

Prefer keydown

New code should listen to keydown (or input events) instead.

📝 Notes

  • MDN: Deprecated — status banner required before What You’ll Learn.
  • Not marked Experimental or Non-standard on the MDN Element page.
  • Prefer keydown or beforeinput for new work.
  • Does not cover all keys (modifiers/arrows often missing).
  • Related learning: keydown, beforeinput, addEventListener(), JavaScript hub.

Deprecated / Legacy Support

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.

Deprecated

Element keypress

Legacy character-key KeyboardEvent; prefer keydown / beforeinput.

Legacy Deprecated
Google Chrome Still fires (legacy; avoid new use)
Legacy
Mozilla Firefox Still fires (legacy; avoid new use)
Legacy
Apple Safari Still fires (legacy; avoid new use)
Legacy
Microsoft Edge Still fires (legacy; avoid new use)
Legacy
Opera Still fires (legacy; avoid new use)
Legacy
Internet Explorer Legacy support
Legacy
keypress Deprecated

Bottom line: Do not start new features on keypress. Migrate existing listeners to keydown (shortcuts) or beforeinput/input (text editing).

Conclusion

keypress is a deprecated character-oriented keyboard event. Know it for legacy reading, then migrate to keydown (or beforeinput / input for editing).

Continue with keyup, keydown, beforeinput, or the JavaScript hub.

💡 Best Practices

✅ Do

  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about keypress

Deprecated character-key event—migrate to keydown.

5
Core concepts
📄 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.

Next: keyup

Learn the release half of the keyboard event pair.

keyup →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful