How to detect Delete
document.addEventListener('keydown', (event) => {
if (event.key === "Delete") {
// Delete was pressed
}
});
Use event.key to react to what the key means (it follows the keyboard layout) and event.code for its physical position, for example WASD in games. keyCode and which are deprecated: they still work, but their values differ between browsers and layouts.
How browsers treat Delete
Delete removes the character after the caret, or the selected item in file managers and lists. On Mac laptops it’s Fn+Delete, because the key labeled “delete” sends Backspace.
In React
<input onKeyDown={(e) => { if (e.key === "Delete") { /* … */ } }} />