Extending Shortcut

Shortcut keys within the editor are managed by the “Shortcut Key Manager”. Each shortcut key can be bound to a message, and when the shortcut key is pressed, the bound message will be triggered.

Defining Shortcut

Defining the shortcuts needs to be done in the contributions.shortcuts field of package.json, as follows:

  1. // package.json
  2. {
  3. "name": "hello-world",
  4. "panels": {
  5. "default": {
  6. "main": "./panel.js"
  7. }
  8. },
  9. "contributions": {
  10. "messages": {
  11. "undo": {
  12. "title": "i18n:hello.messages.undo.title",
  13. "methods": ["say-undo"]
  14. }
  15. },
  16. "shortcuts": [
  17. {
  18. "message": "undo",
  19. "when": "panelName === 'hello-world'",
  20. "win": "ctrl+z",
  21. "mac": "cmd+z",
  22. }
  23. ]
  24. }
  25. }

In this example, we define a shortcut key for the undo operation, which is CTRL + Z on Windows and CMD + Z on macOS.

When the corresponding shortcut key is pressed, the undo message is sent.

Note: This message needs to be defined in contributions.messages, please refer to the documentation Customized Messages.

Parameter descriptions

Below we will see the details of each parameter of contributions.shortcuts.

message

Type {string} Required

Shortcut-bound message that will be sent when this shortcut is triggered. Shortcut pressed messages can only be sent to the current extension.

when

Type {string} Optional

The shortcut will be triggered only under certain conditions.

"when": "PanelName === 'hello-world'" means that the message message will be sent when the shortcut key is pressed when the panel name that gets focus is hello-world.

win

type {string} required

On Windows platform, the keystroke to listen to.

mac

Type {string} Required

On macOS, the keystroke to listen to.