Handling of enter key in an HTML5 dialog
20:08 16 Jul 2021

I've implemented an HTML5 dialog, and as long as the user interacts with the buttons (which emit "close" events), it works fine.

But suppose you press the Enter key. The dialog also closes. I've discovered that the dialog also intercepts keyboard events directly, and close the dialog automatically. This bypasses my processing code.

I can't figure out how to intercept the keyboard event, nor is it clear at what level of the DOM is the the event intercepted and handled. Putting a 'keyup' or similar listener on the dialog does not seem to get the event.

Where are keyboard events intercepted by default when a dialog is in "on" state, and how do I get at them?

javascript html dom dialog modal-dialog