Element: keypress event

Deprecated: This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.

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 methods like addEventListener(), or set an event handler property.

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

onkeypress = (event) => { }

Event type

A KeyboardEvent. Inherits from UIEvent and Event.

Event UIEvent KeyboardEvent

Event properties

This interface also inherits properties of its parents, UIEvent and Event.

KeyboardEvent.altKey Read only

Returns a boolean value that is true if the Alt (Option or ⌥ on macOS) key was active when the key event was generated.

KeyboardEvent.code Read only

Returns a string with the code value of the physical key represented by the event.

KeyboardEvent.ctrlKey Read only

Returns a boolean value that is true if the Ctrl key was active when the key event was generated.

KeyboardEvent.isComposing Read only

Returns a boolean value that is true if the event is fired between after compositionstart and before compositionend.

KeyboardEvent.key Read only

Returns a string representing the key value of the key represented by the event.

KeyboardEvent.location Read only

Returns a number representing the location of the key on the keyboard or other input device. A list of the constants identifying the locations is shown in Keyboard locations.

KeyboardEvent.metaKey Read only

Returns a boolean value that is true if the Meta key (on Mac keyboards, the ⌘ Command key; on Windows keyboards, the Windows key (⊞)) was active when the key event was generated.

KeyboardEvent.repeat Read only

Returns a boolean value that is true if the key is being held down such that it is automatically repeating.

KeyboardEvent.shiftKey Read only

Returns a boolean value that is true if the Shift key was active when the key event was generated.

Examples

addEventListener keypress example

This example logs the KeyboardEvent.code value whenever you press a key after focussing 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}`;
}

onkeypress equivalent

js
input.onkeypress = logKey;

Specifications

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

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
keypress event
Deprecated
Chrome – Full support
Chrome 1 (Release date: 2008-12-11)
footnote
footnote Chrome does not fire the keypress event for known keyboard shortcuts. Which keyboard shortcuts are known depends on the user's system. Use the keydown event to implement keyboard shortcuts.
Edge – Full support
Edge 12 (Release date: 2015-07-29)
footnote Full support
Firefox – Full support
Firefox 6 (Release date: 2011-08-16)
footnote
footnote As of Firefox 65, the keypress event is no longer fired for non-printable keys, except for the Enter key, and the Shift + Enter and Ctrl + Enter key combinations (these were kept for cross-browser compatibility purposes).
Opera – Full support
Opera 12.1 (Release date: 2012-11-20)
footnote
footnote Opera does not fire the keypress event for known keyboard shortcuts. Which keyboard shortcuts are known depends on the user's system. Use the keydown event to implement keyboard shortcuts.
Safari – Full support
Safari 1.3 (Release date: 2005-04-15)
footnote Full support
Chrome Android – Full support
Chrome Android 18 (Release date: 2012-06-27)
footnote
footnote Chrome Android does not fire the keypress event for known keyboard shortcuts. Which keyboard shortcuts are known depends on the user's system. Use the keydown event to implement keyboard shortcuts.
Firefox for Android – Full support
Firefox for Android 6 (Release date: 2011-08-16)
footnote
footnote As of Firefox for Android 65, the keypress event is no longer fired for non-printable keys, except for the Enter key, and the Shift + Enter and Ctrl + Enter key combinations (these were kept for cross-browser compatibility purposes).
Opera Android – Full support
Opera Android 12.1 (Release date: 2012-10-09)
footnote
footnote Opera does not fire the keypress event for known keyboard shortcuts. Which keyboard shortcuts are known depends on the user's system. Use the keydown event to implement keyboard shortcuts.
Safari on iOS – Full support
Safari on iOS 1 (Release date: 2007-06-29)
footnote Full support
Samsung Internet – Full support
Samsung Internet 1 (Release date: 2013-04-27)
footnote
footnote Samsung Internet does not fire the keypress event for known keyboard shortcuts. Which keyboard shortcuts are known depends on the user's system. Use the keydown event to implement keyboard shortcuts.
WebView Android – Full support
WebView Android 1 (Release date: 2008-09-23)
footnote
footnote Chrome does not fire the keypress event for known keyboard shortcuts. Which keyboard shortcuts are known depends on the user's system. Use the keydown event to implement keyboard shortcuts.
WebView on iOS – Full support
WebView on iOS 1 (Release date: 2007-06-29)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Deprecated. Not for use in new websites.
See implementation notes.

See also