Element: keypress event
{{APIRef("UI Events")}} {{deprecated_header}}
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
beforeinputorkeydowninstead.
The event bubbles. It can reach {{domxref("Document")}} and {{domxref("Window")}} .
Syntax
Use the event name in methods like {{domxref("EventTarget.addEventListener", "addEventListener()")}} , or set an event handler property.
addEventListener("keypress", (event) => { })
onkeypress = (event) => { }
Event type
A {{domxref("KeyboardEvent")}} . Inherits from {{domxref("UIEvent")}} and {{domxref("Event")}} .
{{InheritanceDiagram("KeyboardEvent")}}
Event properties
This interface also inherits properties of its parents, {{domxref("UIEvent")}} and {{domxref("Event")}} .
-
{{domxref("KeyboardEvent.altKey")}}{{ReadOnlyInline}}- : Returns a boolean value that is
trueif the Alt (Option or ⌥ on macOS) key was active when the key event was generated.
- : Returns a boolean value that is
-
{{domxref("KeyboardEvent.code")}}{{ReadOnlyInline}}- : Returns a string with the code value of the physical key represented by the event.
-
{{domxref("KeyboardEvent.ctrlKey")}}{{ReadOnlyInline}}- : Returns a boolean value that is
trueif the Ctrl key was active when the key event was generated.
- : Returns a boolean value that is
-
{{domxref("KeyboardEvent.isComposing")}}{{ReadOnlyInline}}- : Returns a boolean value that is
trueif the event is fired between aftercompositionstartand beforecompositionend.
- : Returns a boolean value that is
-
{{domxref("KeyboardEvent.key")}}{{ReadOnlyInline}}- : Returns a string representing the key value of the key represented by the event.
-
{{domxref("KeyboardEvent.location")}}{{ReadOnlyInline}}- : 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.
-
{{domxref("KeyboardEvent.metaKey")}}{{ReadOnlyInline}}- : Returns a boolean value that is
trueif the Meta key (on Mac keyboards, the ⌘ Command key; on Windows keyboards, the Windows key (⊞)) was active when the key event was generated.
- : Returns a boolean value that is
-
{{domxref("KeyboardEvent.repeat")}}{{ReadOnlyInline}}- : Returns a boolean value that is
trueif the key is being held down such that it is automatically repeating.
- : Returns a boolean value that is
-
{{domxref("KeyboardEvent.shiftKey")}}{{ReadOnlyInline}}- : Returns a boolean value that is
trueif the Shift key was active when the key event was generated.
- : Returns a boolean value that is
Examples
addEventListener keypress example
This example logs the {{domxref("KeyboardEvent.code")}} value whenever you press a key after focussing the {{htmlelement("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
<div>
<label for="sample">Focus the input and type something:</label>
<input type="text" name="text" id="sample" />
</div>
<p id="log"></p>
const log = document.getElementById("log");
const input = document.querySelector("input");
input.addEventListener("keypress", logKey);
function logKey(e) {
log.textContent += ` ${e.code}`;
}
{{EmbedLiveSample("addEventListener_keypress_example")}}
onkeypress equivalent
input.onkeypress = logKey;
Specifications
{{Specifications}}
Browser compatibility
{{Compat}}