Enter key code
Enter sends event.key "Enter" and keyCode 13. Both Enter keys share them; event.code and event.location tell them apart.
Every value at a glance
| Key | event.code | event.key | keyCode | event.location |
|---|---|---|---|---|
| Enter / Return | Enter | Enter | 13 | 0 (Standard) |
| Numpad Enter | NumpadEnter | Enter | 13 | 3 (Number pad) |
keyCode as Chrome and Edge send it on Windows. The Mac column (Chrome and Safari on a Mac) and the Firefox column show only numbers that differ; some keys also differ by system, as the text below says. event.which is always the same number as keyCode. event.key is for a US layout without Shift.
Press it here
Click here, then press Enter
Using only a keyboard? Press Esc, then Tab, to leave the key box.
Code to react to Enter
document.addEventListener('keydown', (event) => {
if (event.key === 'Enter') {
// Your code here
}
});document.addEventListener('keydown', (event) => {
if (event.code === 'Enter') {
// Your code here
}
});document.addEventListener('keydown', (event) => {
if (event.keyCode === 13) {
// Your code here
}
});Which value to use
Check event.key === 'Enter'. It is the same for the main Enter key and the one on the number pad, on every layout, and also on phone keyboards that send a real Enter.
Use event.code only when the two keys must do different things: Enter is the big key, NumpadEnter is the one on the number pad. The Mac return key is the same key: it sends Enter too.
keyCode 13 still arrives in every browser, so old code keeps working, but it is deprecated.
What trips people up
Enter sends the message while someone is still typing Japanese, Chinese or Korean
With an input method, Enter first confirms the character being built. Your keydown handler sees that Enter too. Skip it while a character is being composed:
if (event.isComposing || event.keyCode === 229) return;
Check both: Safari sends the confirming Enter with isComposing false but keyCode 229.
Pressing Enter in a text field submits the form
That is the browser’s implicit submission. Call event.preventDefault() in the keydown handler if Enter should do something else, or listen for the form’s submit event instead of the key, which also catches the Go button on phone keyboards.
The handler runs twice or many times
Holding Enter repeats the keydown. Ignore repeats with if (event.repeat) return; when Enter starts something that should happen once, such as sending a message.
keypress or keyup doesn’t behave like keydown
keypress is deprecated and fires only for keys that type something; it still fires for Enter, but don’t build on it. keyup comes after the browser has already submitted the form or added the new line, too late to stop it. Use keydown.
Enter on a phone shows “Go”, “Next” or “Send”
The label is up to you: add enterkeyhint="send" (or go, next, search, done) to the input. Android keyboards send most keys as keyCode 229, but Enter usually arrives as a real Enter with keyCode 13.
Shift+Enter and Ctrl+Enter
Chat boxes usually send on Enter and add a new line on Shift + Enter: check event.shiftKey. Ctrl + Enter (Cmd + Enter on a Mac) is the common “send anyway” shortcut: check event.ctrlKey || event.metaKey. In a <textarea>, Enter adds a new line unless you call preventDefault().