Skip to content

Keyboard handling

The PrismEditor.keyCommandMap record maps KeyboardEvent.key to a function called when that key is pressed. This can be useful to add a simple keyboard shortcut without adding an entire keydown handler.

The defaultCommands() extension registers multiple commands to keyCommandMap. These can be overridden after the defaultCommands() is added.

If the handler returns true, e.preventDefault() and e.stopImmediatePropagation() get called automatically.

Multiple extensions add keydown event handlers to the textarea instead of using the keyCommandMap. Some of them also use capture mode. This is because if a handler in the keyCommandMap returns true, e.stopImmediatePropagation() gets called. Using capture mode ensures the handler runs before any keyCommandMap handlers.

There’s also an inputCommandMap mapping user input (InputEvent.data) to functions called when that input is typed. This is used by defaultCommands() to automatically close brackets, quotes, and XML tags. If the handler returns true, e.preventDefault() and e.stopImmediatePropagation() get called in the beforeinput handler.