The onkeypress attribute is an inline event handler for the keypress event. It historically ran when a letter, number, punctuation, symbol, or Enter was pressed on a focused element (for example an <input>).
Modifier-only keys such as Alt, Shift, Ctrl, Meta, and Esc alone do not fire keypress. For new work, prefer onkeydown, oninput, or onbeforeinput.
Deprecated.
MDN marks the keypress event deprecated. Browsers may still fire it for compatibility — do not build new features on onkeypress.
Syntax
HTML Syntax
syntax.html
<element onkeypress="script">
script is JavaScript that runs when keypress fires. Pass event to read event.key. Modern equivalent: element.addEventListener("keydown", handler) or "input".
Values
Attribute Values
The attribute value is a script. Common legacy patterns:
Value
Description
Try it
handler()
Call a named function when a character key is pressed
Uses input — preferred over deprecated onkeypress.
How It Works
Listen to input. Fires whenever the value changes, including paste.
Clean the string. Strip disallowed characters from field.value.
Add pattern. Native constraint validation helps forms and assistive tech.
Prefer this path. Keep onkeypress only when reading older tutorials or codebases.
Edge Cases & Pitfalls
Most surprises come from treating keypress like a complete keyboard API.
deprecated
Do not start new features on it
Specs and MDN recommend keydown or beforeinput/input. Compatibility firing can still change.
keys
Not every key fires keypress
Escape, arrows, and many modifiers usually skip it. Shortcuts belong on onkeydown.
paste
Paste bypasses character filters
Blocking keys does not clean pasted text. Sanitize on input and validate on the server.
legacy API
Avoid keyCode / which
Use event.key (character or named key) and event.code (physical key) instead.
a11y
Do not trap Tab or Escape
Never use keyboard handlers to break focus order. Prefer HTML constraints and clear error text when blocking input.
Compatibility
Browser Support
onkeypress is a legacy HTML event handler. Engines may still fire keypress for old pages.
✓ Deprecated · Legacy compatibility
onkeypress
Still widely recognized for printable keys, but deprecated. Prefer keydown / input for new projects. Behavior for non-character keys was never consistent.
LegacyNot for new work
Google ChromeFires for compatibility
Deprecated*
Mozilla FirefoxFires for compatibility
Deprecated*
Apple SafariFires for compatibility
Deprecated*
Microsoft EdgeFires for compatibility
Deprecated*
OperaFires for compatibility
Deprecated*
Samsung InternetFires for compatibility
Deprecated*
Internet ExplorerLegacy support
Legacy*
onkeypressDeprecated — use keydown/input
Bottom line: *Learn onkeypress for old code. Build new keyboard and input logic with onkeydown, onkeyup, or oninput.
Remember
Key Takeaways
Role: legacy inline handler for character-oriented keypress events.
Status: deprecated — recognize it, do not start new features on it.
Replace with:onkeydown for keys/shortcuts, oninput for value changes.
API: use event.key, not keyCode.
One line: study onkeypress for legacy pages — write keydown / input for new ones.
Frequently Asked Questions
It is an inline event handler for the keypress event. Historically it ran when a printable character key (or Enter) was pressed in a focused element. The keypress event is deprecated.
Yes. MDN and modern specs deprecate keypress. Prefer onkeydown, beforeinput, or oninput for new projects. Learn onkeypress to read legacy code.
onkeydown fires for almost every key, including Escape and arrows. onkeypress mainly targeted character keys and is deprecated. Use onkeydown with event.key for reliable keyboard handling.
Usually no. Modifier-only keys and most navigation keys do not fire keypress. Use onkeydown for those.
No. Paste does not go through keypress. Validate the field value with oninput, or use pattern and server checks.
Use event.key (and event.code when you need the physical key). keyCode and which are deprecated.