🔑 Keycode

👋 FYI, this note is over 6 months old. Some of the content may be out of date.
On this page
document.addEventListener('keydown', (event: Event) => {
if (!(event instanceof KeyboardEvent)) {
return
}
if (event.target instanceof Node && isFormField(event.target)) {
return
}
if (event.isComposing || event.keyCode === 229) {
// do something
}
})

Alt link: http://keycode.info/

isFormField() util Jump to heading

export function isFormField(element: Node): boolean {
if (!(element instanceof HTMLElement)) {
return false
}

const name = element.nodeName.toLowerCase()
const type = (element.getAttribute('type') || '').toLowerCase()
return (
name === 'select' ||
name === 'textarea' ||
(name === 'input' && type !== 'submit' && type !== 'reset') ||
element.isContentEditable
)
}

← Back home