Delete key code

Delete (forward delete) sends event.key "Delete", event.code "Delete" and keyCode 46.

Every value at a glance

Values for Delete
Keyevent.codeevent.keykeyCodeevent.location
Delete / Forward DeleteDeleteDelete460 (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 Delete

Using only a keyboard? Press Esc, then Tab, to leave the key box.

Code to react to Delete

Recommended: match event.key
document.addEventListener('keydown', (event) => {
  if (event.key === 'Delete') {
    // Your code here
  }
});
Match the key's place: event.code
document.addEventListener('keydown', (event) => {
  if (event.code === 'Delete') {
    // Your code here
  }
});
Old code that still uses keyCode (deprecated)
document.addEventListener('keydown', (event) => {
  if (event.keyCode === 46) {
    // Your code here
  }
});

Which value to use

Check event.key === 'Delete'. It catches the Delete key and also the numpad’s Del key: with Num Lock off on Windows, the numpad’s . key sends key "Delete" and keyCode 46, but event.code stays "NumpadDecimal". So event.code === 'Delete' misses it.

keyCode 46 still arrives in every browser, but it is deprecated. Internet Explorer and Firefox 36 and earlier sent "Del" instead of "Delete".

What trips people up

Mac users can’t find the Delete key

The big key labelled delete on a Mac is Backspace. Most Mac laptops have no Delete key at all: fn + delete sends "Delete". Full-size Apple keyboards have a separate key marked ⌦ or “delete” with an arrow. If Delete removes a selected item in your app, also accept Backspace, which is what Mac users will press first.

Delete removes an item while the user is typing

A page-wide shortcut that deletes the selected row also fires when someone presses Delete in a text field. Check where focus is first:

if (event.key === 'Delete' && !event.target.closest('input, textarea, [contenteditable]')) {
  removeSelected();
}

Delete does nothing on a phone

Phone keyboards have no forward delete key, and Android keyboards send most keys as keyCode 229 anyway. In a text field, listen for beforeinput and check event.inputType === 'deleteContentForward'. Ctrl + Delete gives deleteWordForward.

keyCode 46 in old code means something else

46 is also the character code of ., so keypress handlers that read charCode or which see 46 for a typed full stop. That is why . and Delete get mixed up in old code. Delete never fires keypress in current browsers; use keydown and event.key.

Delete versus Backspace

Delete removes the character after the cursor; Backspace removes the one before it. With text selected, both remove the selection. In an editor, Delete at the very end of the text does nothing, and Backspace at the very start does nothing, so the text doesn’t change and no input event fires.