Accepted answer

This is correct because event keydown is attached to an element. The default context come back useful when you are working with inputs. When you need a global event (as events are propagated top/down) you can rely on window object or document object:

  document.addEventListener('keydown', (event) => {
    const keyName = event.key;
    alert('keydown event\n\n' + 'key: ' + keyName);

  // Remove the listener before unmount

More Query from same tag