GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • MDN
  • HTML: Markup language
  • Elements
  • Global attributes
  • Attributes
  • See all…
  • Responsive images
  • HTML cheatsheet
  • Date & time formats
  • See all…
  • SVG
  • MathML
  • XML
  • CSS: Styling language

This view

  • Original page
  • New address

GeezerVison is showing developer.mozilla.org — Element: keypress event - Web APIs | MDN

Element: keypress event

Deprecated

Avoid using this feature in new projects. This feature may be a candidate for removal from web standards or browsers.

  • See full compatibility
  • Learn more

The keypress event is fired when a letter, number, punctuation, or symbol key is pressed, or else when the Enter key is pressed — including when the Enter key is pressed in combination with the Shift key or Ctrl key. Otherwise, when a modifier key such as the Alt, Shift, Ctrl, Meta, Esc, or Option key is pressed in isolation, the keypress event is not fired.

Warning: Since this event has been deprecated, you should use beforeinput or keydown instead.

The event bubbles. It can reach Document and Window.

Syntax

Use the event name in addEventListener():

js
addEventListener("keypress", (event) => { })

Alternatively, assign to the event handler property of the interface:

js
onkeypress = (event) => { }

Event type

A KeyboardEvent. Inherits from UIEvent and Event.

Examples

addEventListener keypress example

This example logs the KeyboardEvent.code value whenever you press a key after focusing the <input> element.

To see which keys cause a keypress event to fire, and which keys don't, try pressing the following:

  • letter keys, number keys, and punctuation keys
  • symbol keys such as the $, +, =, %, and + keys
  • modifier keys such as the Alt, Shift, Ctrl, Meta, Esc, Option, or ⌘ keys
  • the Enter key
  • the Enter key in combination with the Shift or Ctrl keys
  • the Enter key in combination with modifier keys other than the Shift or Ctrl keys
html
<div>
  <label for="sample">Focus the input and type something:</label>
  <input type="text" name="text" id="sample" />
</div>
<p id="log"></p>
js
const log = document.getElementById("log");
const input = document.querySelector("input");

input.addEventListener("keypress", logKey);

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

Using the onkeypress event handler property

You can also use the onkeypress event handler property (but note that this replaces other event listeners added using onkeypress, if any):

js
input.onkeypress = logKey;

Specifications

Specification
UI Events
# event-type-keypress
HTML
# handler-onkeypress

Browser compatibility

See also

  • The Document interface, which the event also targets.
  • Related events:
    • input
    • keydown
    • keyup

Help improve MDN

Learn how to contribute

This page was last modified on Nov 10, 2026 by MDN contributors.

View this page on GitHub • Report a problem with this content
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.