Keyboard shortcuts for a prompt
Ctrl+Enter (or Cmd+Enter on a Mac) sends the prompt and Escape clears it. Every other key keeps working as usual.
Write handleKey(event) and register it with document.addEventListener('keydown', handleKey).
EnterwithctrlKeyormetaKey: callrunPrompt()and stop the default action withevent.preventDefault().Escape: empty the#promptfield.- Any other key, plain
Enterincluded: do nothing.
runPrompt() already exists and counts runs in runCount.
Challenges 0/4
- Ctrl+Enter runs once and prevents the default action
- Cmd+Enter (metaKey) runs too
- Plain Enter and other keys run nothing
- Escape clears the field
function handleKey(event) {
// Ctrl/Cmd + Enter: runPrompt() and event.preventDefault()
// Escape: clear #prompt
}
document.addEventListener('keydown', handleKey);Tab indents, Ctrl+Z undoes. To leave the editor with the keyboard: Esc, then Tab.
Why this matters
Someone who uses a chat all day does not want to leave the keyboard. Ctrl+Enter to send and Escape to clear are what people expect from a prompt box, and the hard part is not adding them but not breaking the rest: Enter must still insert a new line, Cmd must work on a Mac, and the shortcut must not fire twice. It is the same shortcut the editor on this page uses to run your code. If you would rather have an interface that already does it, see Open WebUI for a 10-person office.
Go deeper: the MDN reference →
This in production, with your data? Let's talk for 15 minutes →