shortcutDef

Release 2.4.0+

Defines keyboard shortcuts for a custom plugin.

Top-level Keys

NameTypeDescription
keyobjectPer-OS shortcut bindings.
optionobjectWhat runs when the shortcut fires (action / function), plus other options.

key

NameTypeDescription
windowsstringWindows shortcut
macstringMac shortcut

option

NameTypeDescription
actionstringEditor Action to run.
paramsobjectArguments passed to the action.
onExecutefunctionRelease 2.15.2211+ Function to run. If both action and onExecute are set, only onExecute runs.
focusImebooleanWhether to refocus the editor after the shortcut fires (default true).
overridebooleanRelease 3.2.2507+ When true, overrides an existing shortcut (default false).

Shortcut Strings

Combine keys with +. Examples:

  • Ctrl + Shift + ; → 'Ctrl+Shift+;'
  • Ctrl + Alt + N → 'Ctrl+Alt+N'
// Ctrl+Shift+;
shortcutDef: {
  key: { windows: 'Ctrl+Shift+;', mac: 'Cmd+Shift+;' },
  option: { /* ... */ }
}

// Ctrl+Alt+N
shortcutDef: {
  key: { windows: 'Ctrl+Alt+N', mac: 'Cmd+Alt+N' },
  option: { /* ... */ }
}

Supported Keys

Ctrl, Shift, Alt (Mac: Option), Cmd (Mac only), 0–9, A–Z, F1–F12, ; + , - . / ` [ \ ] ', Backspace, Tab, Enter, Esc, Space, PageUp, PageDown, End, Home, Insert, Delete, Left, Up, Right, Down.

Examples

Run an editor action

shortcutDef: {
  key: { /* ... */ },
  option: { action: 'increaseFontSize' }
}

Action with params

shortcutDef: {
  key: { /* ... */ },
  option: { action: 'align', params: ['center'] }
}

Run a function

shortcutDef: {
  key: { /* ... */ },
  option: {
    /**
     * Receives the editor instance and the keyboard event.
     * @param {SynapEditor} editor
     * @param {KeyboardEvent} keyEvent
     */
    onExecute: function (editor, keyEvent) {
      window.alert('Shortcut test!');
    }
  }
}

Override an existing shortcut

Release 3.2.2507+

Without override, attempting to register a shortcut that's already bound shows a notification and the registration is skipped:

shortcut_notification.png

Setting override: true replaces the existing binding.

shortcutDef: {
  key: { /* ... */ },
  option: { /* ... */ },
  override: true
}

Single vs. Multiple Shortcuts

Single

shortcutDef: {
  key: { /* ... */ },
  option: { /* ... */ }
}

Multiple

shortcutDef: [
  {
    key: { /* ... */ },
    option: { /* ... */ }
  }
  // ...
]