How to detect Backslash (\)
document.addEventListener('keydown', (event) => {
if (event.key === "\\") {
// Backslash (\) 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 Backslash (\)
The backslash key’s position varies most between layouts; ISO keyboards have an extra key next to Left Shift (IntlBackslash).
In React
<input onKeyDown={(e) => { if (e.key === "\\") { /* … */ } }} />