Skip to content

Hotkeys

The prism-code-editor/commands entrypoint exports many editing commands. These commands receive an editor and return a boolean or void. A falsy value indicates the command doesn’t apply (most commands don’t apply in readOnly mode, for example), while true means it successfully executed.

To bind a command to a specific key, use addEditorHotkey(). This will add the command at the end of a list of callbacks for the specified key, or at the beginning if the 4th highPrecedence parameter is set to true. When the command is executed, it gets passed the editor and KeyboardEvent as arguments.

When multiple commands are registered for one key, they are ran in order until one returns true, in which case the event is considered “handled” and stopImmediatePropagation() along with preventDefault().

Multiple different keys are usually tried when pressing punctuation keys or Shift + a number key for better compatibility across different keyboard layouts and platforms. The editor below will show all keys tried for the most recent keypress. This is useful for debugging why a command isn’t executing. For better visibility, " " has been replaced with "space".

Previous keys:

The editorCommands() can be used to add commands from a keymap, such as defaultKeymap or a custom keymap if you’re dissatisfied with the default one. Note that multiple instances of editorCommands() should not be added to an editor, because it does more than just adding the hotkeys; instead, merge the keymaps or add extra commands manually with addEditorHotkey().

Keys are strings that can appear in KeyboardEvent.key, preceded by any number of modifiers, all separated by +, such as ctrl+shift+enter. Hotkeys are case-insensitive.

To ensure hotkeys are matched regardless of the order of modifiers, the modifiers are normalized using normalizeKey() to a bitmask according to the following list:

  • alt1
  • ctrl or control2
  • meta or cmd4
  • shift8
  • mod4 on Mac, 2 otherwise

If wanted, bitmasks can be used directly in combination with strings for modifiers. Here are some examples:

The 4th precedence parameter can be used to control the execution order when multiple hotkeys are registered to the same key. The precedence is a positive integer that defaults to 2, where lower numbers yield higher precedence. A precedence of 1 should be used when conditionally overriding existing commands. Precedence 0, (the highest) should be used with caution since it can override sequential commands if the key is included in a sequence. When commands have the same precedence, they execute in the order they were added.

When adding a hotkey with "any" as the key, it will run for any keydown event, after commands for the current key even if a one returned true. Note that returning true from the command won’t mark the event as “handled”.

This library also supports adding sequential hotkeys where multiple keys must be pressed one after another to execute the command. To bind a command to a sequence, use addEditorHotkeySequence().

The last, optional parameter (type HotkeySequenceOptions) configures some behavior of the sequential hotkey. The example below shows the defaults:

The first key in the sequence gets bound to a command that starts the sequence. When the sequence is advanced, a hotkey for the next key in the sequence gets added until the final step, when the command gets executed instead. If another key was pressed or the timeout expired between steps, the sequence is reset. Note that pressing modifier keys like Alt or Shift won’t reset the sequence. If multiple sequences share the same steps, they will advance simultaneously.

All functions that add hotkeys return a cleanup function to remove the hotkey.

The utilities for adding and running hotkeys are exported, meaning you can easily add hotkeys to your own application with full control over where listeners are added and how events are filtered.

The getKeysFromEvent() utility can be used to check if a key matches a keyboard event. The return values from getKeysFromEvent(e) and normalizeKey() can be cached for better performance when checking multiple hotkeys.

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

getKeysFromEvent() can be used to to capture shortcuts pressed by users for customizable key bindings.

This library exports a formatHotkey() utility to format hotkeys before displaying them to users. In addition, you’ll find lists of keyboard shortcuts and descriptions for defaultKeymap, autoComplete(), and searchWidget(). The following example shows how all shortcuts can be rendered with JSX:

Below you can see all shortcuts rendered in lists.