Service for binding keyboard shortcuts to execute commands

A keybinding is a sequence or key combination on a computer keyboard which invokes commands. This service supports key bindings as a way of responding to individual keys typed by a user. The key combination can be pressing a single key or a sequence of keys one after the other.

The service works together with the CommandBus to execute the commands and a command needs to already be registered on the command bus in order to be bound as a keyboard shortcut.

The following example registers the command MyCommand to be handled by MyHandler and creates a keybinding for it. The bind method takes the key combination and command object as input.

Later, if the keybinding no longer is needed it can be removed with the unbind method.

Example

// The command bus needs to have a handler registered for the command
// before it can be bound to a key sequence
const handler = new MyHandler();
commandBus.register(MyCommand, handler);

// Bind 'ctrl+s' to trigger the command
const command = new MyCommand();
keybindingRegistry.bind('ctrl+s', command);

// Remove the binding when we no longer need it to trigger the command
keybindingRegistry.unbind('ctrl+s')

Note

This service is work in progress

Hierarchy

  • KeybindingRegistry

Properties

bind: ((keys: string, command: object) => void)

Type declaration

    • (keys: string, command: object): void
    • Bind a command to a specific key combination to add it to the registry

      Throws

      Will throw an error if the command is invalid or not registered

      Parameters

      • keys: string

        the string representation of keys

      • command: object

        the command to trigger when keys are pressed

      Returns void

getKeybindings: (() => Keybinding[])

Type declaration

    • (): Keybinding[]
    • Get a list of the key bindings that are currently in the registry

      Returns

      the list of command and its specific key combination

      Returns Keybinding[]

isBound: ((keys: string, command?: object) => boolean)

Type declaration

    • (keys: string, command?: object): boolean
    • Checks if a key combination or a specific keybinding is already bound

      Returns

      true if key combination already exists, else false

      Parameters

      • keys: string

        the string representation of a key combination

      • Optional command: object

        the command connected to the key combination

      Returns boolean

unbind: ((keys: string, command: object) => void)

Type declaration

    • (keys: string, command: object): void
    • Unbind a keybinding to remove it from the registry

      Parameters

      • keys: string

        the string representation of the key combination

      • command: object

        the command connected to the key combination

      Returns void