Home, End, Page Up and Page Down key codes
These keys send event.key "Home", "End", "PageUp" and "PageDown", and keyCode 36, 35, 33 and 34.
Every value at a glance
| Key | event.code | event.key | keyCode | event.location |
|---|---|---|---|---|
| Home | Home | Home | 36 | 0 (Standard) |
| End | End | End | 35 | 0 (Standard) |
| Page Up | PageUp | PageUp | 33 | 0 (Standard) |
| Page Down | PageDown | PageDown | 34 | 0 (Standard) |
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 Home, End, Page Up or Page Down
Using only a keyboard? Press Esc, then Tab, to leave the key box.
Code to react to Home
document.addEventListener('keydown', (event) => {
if (event.key === 'Home') {
// Your code here
}
});document.addEventListener('keydown', (event) => {
if (event.code === 'Home') {
// Your code here
}
});document.addEventListener('keydown', (event) => {
if (event.keyCode === 36) {
// Your code here
}
});Which value to use
Check event.key: "Home", "End", "PageUp" or "PageDown". It also matches what a Mac laptop sends for Fn plus an arrow, and the number pad keys with Num Lock off.
Use event.code only to tell the number pad apart. With Num Lock off, number pad 7 sends Home, 1 End, 9 Page Up and 3 Page Down, with the same key and keyCode as the real keys. Their code stays Numpad7, Numpad1, Numpad9 or Numpad3, and location is 3.
The keyCode numbers are not in key order: Page Up 33, Page Down 34, End 35, Home 36. Every browser still sends them, but they are deprecated.
What trips people up
Mac users can’t find the keys
Mac laptops and Apple’s compact keyboards have no Home, End, Page Up or Page Down keys. Fn + Left sends Home, Fn + Right End, Fn + Up Page Up and Fn + Down Page Down. Your page sees the real keys, so name both in your help text: Home (Fn + Left on a Mac).
Pressing the key scrolls the page
Scrolling to the top, the bottom or by one screen is the default action. Call event.preventDefault() in keydown only when your own widget uses the key, such as a list or a slideshow. In a text field on Windows and Linux, Home and End move the cursor to the start or end of the line; leave those alone. On a Mac, Cmd + Left and Cmd + Right do that, and Home and End usually scroll instead.
Ctrl+Page Up and Ctrl+Page Down can’t be stopped
Browsers use them to switch tabs. Chrome and Edge don’t pass them to the page, and no browser lets preventDefault() stop them. Pick another shortcut. Plain Page Up and Page Down do arrive.
A presentation remote does nothing
Many presentation clickers act as a keyboard and send Page Down for next and Page Up for back. Some send the arrows instead. A slideshow should accept both: PageDown or ArrowRight for next, PageUp or ArrowLeft for back.
Lists and menus
In a custom list, menu, grid or tab bar, users of keyboards and screen readers expect Home to go to the first item and End to the last. Page Up and Page Down usually move by one visible page of items. Add these keys next to the arrows when you build such a widget, and call preventDefault() so the page doesn’t scroll as well.