Skip to content

Commit b3c6c88

Browse files
Document that the Enter key causes the “keypress” event to fire (#37817)
Document that the Enter key causes the keypress event to fire This change updates the page for the “keypress” event to match actual behavior in browsers — by changing the page to state that the event fires when the Enter key is pressed — not only when a key that produces a character value is pressed. See w3c/uievents#183 (comment) and w3c/uievents#266 (comment) and w3c/uievents#392 Otherwise, without this change, the information on the “keypress” page incorrectly doesn’t match actual browser behavior — in that the page states that the “keypress” event fires _only_ when a key that produces a character value are pressed, while in fact browsers also fire the event when the Enter key is pressed.
1 parent b3255d8 commit b3c6c88

File tree

1 file changed

+10
-3
lines changed
  • files/en-us/web/api/element/keypress_event

1 file changed

+10
-3
lines changed

files/en-us/web/api/element/keypress_event/index.md

+10-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ browser-compat: api.Element.keypress_event
1010

1111
{{APIRef}} {{deprecated_header}}
1212

13-
The **`keypress`** event is fired when a key that produces a character value is pressed down.
14-
15-
Examples of keys that produce a character value are alphabetic, numeric, and punctuation keys. Examples of keys that don't produce a character value are modifier keys such as <kbd>Alt</kbd>, <kbd>Shift</kbd>, <kbd>Ctrl</kbd>, or <kbd>Meta</kbd>.
13+
The **`keypress`** event is fired when a [letter, number, punctuation, or symbol](https://w3c.github.io/uievents/#unicode-character-categories) key is pressed, or else when the <kbd>Enter</kbd> key is pressed — including when the <kbd>Enter</kbd> key is pressed in combination with the <kbd>Shift</kbd> key or <kbd>Ctrl</kbd> key. Otherwise, when a modifier key such as the <kbd>Alt</kbd>, <kbd>Shift</kbd>, <kbd>Ctrl</kbd>, <kbd>Meta</kbd>, <kbd>Esc</kbd>, or <kbd>Option</kbd> key is pressed in isolation, the `keypress` event is _not_ fired.
1614

1715
> [!WARNING]
1816
> Since this event has been deprecated, you should use [`beforeinput`](/en-US/docs/Web/API/Element/beforeinput_event) or [`keydown`](/en-US/docs/Web/API/Element/keydown_event) instead.
@@ -71,6 +69,15 @@ _This interface also inherits properties of its parents, {{domxref("UIEvent")}}
7169

7270
This example logs the {{domxref("KeyboardEvent.code")}} value whenever you press a key after focussing the {{htmlelement("input")}} element.
7371

72+
To see which keys cause a `keypress` event to fire, and which keys don't, try pressing the following:
73+
74+
- letter keys, number keys, and punctuation keys
75+
- symbol keys such as the <kbd>$</kbd>, <kbd>+</kbd>, <kbd>=</kbd>, <kbd>%</kbd>, and <kbd>+</kbd> keys
76+
- modifier keys such as the <kbd>Alt</kbd>, <kbd>Shift</kbd>, <kbd>Ctrl</kbd>, <kbd>Meta</kbd>, <kbd>Esc</kbd>, <kbd>Option</kbd>, or <kbd>⌘</kbd> keys
77+
- the <kbd>Enter</kbd> key
78+
- the <kbd>Enter</kbd> key in combination with the <kbd>Shift</kbd> or <kbd>Ctrl</kbd> keys
79+
- the <kbd>Enter</kbd> key in combination with modifier keys other than the <kbd>Shift</kbd> or <kbd>Ctrl</kbd> keys
80+
7481
```html
7582
<div>
7683
<label for="sample">Focus the input and type something:</label>

0 commit comments

Comments
 (0)