Skip to main content

Class: ShortcutRegistry

Defined in: packages/blockly/core/shortcut_registry.ts:26

Class for the registry of keyboard shortcuts. This is intended to be a singleton. You should not create a new instance, and only access this class from ShortcutRegistry.registry.

Properties index

PropertyDescription
registry-

Methods index

MethodDescription
resetClear and recreate the registry and keyMap.
registerRegisters a keyboard shortcut.
unregisterUnregisters a keyboard shortcut registered with the given name. This will also remove any key mappings that reference this shortcut.
addKeyMappingAdds a mapping between a keycode and a keyboard shortcut.
removeKeyMappingRemoves a mapping between a keycode and a keyboard shortcut.
removeAllKeyMappingsRemoves all the key mappings for a shortcut with the given name. Useful when changing the default key mappings and the key codes registered to the shortcut are unknown.
setKeyMapSets the key map. Setting the key map will override any default key mappings.
getKeyMapGets the current key map.
getRegistryGets the registry of keyboard shortcuts.
onKeyDownHandles key down events.
getShortcutNamesByKeyCodeGets the shortcuts registered to the given key code.
getKeyCodesByShortcutNameGets the serialized key codes that the shortcut with the given name is registered under.
createSerializedKeyCreates the serialized key code that will be used in the key map.

Properties

registry

readonly static registry: ShortcutRegistry;

Defined in: packages/blockly/core/shortcut_registry.ts:27

Methods

reset()

reset(): void;

Defined in: packages/blockly/core/shortcut_registry.ts:41

Clear and recreate the registry and keyMap.

Returns

void


register()

register(shortcut, allowOverrides?): void;

Defined in: packages/blockly/core/shortcut_registry.ts:54

Registers a keyboard shortcut.

Parameters

ParameterTypeDescription
shortcutKeyboardShortcutThe shortcut for this key code.
allowOverrides?booleanTrue to prevent a warning when overriding an already registered item.

Returns

void

Throws

if a shortcut with the same name already exists.


unregister()

unregister(shortcutName): boolean;

Defined in: packages/blockly/core/shortcut_registry.ts:76

Unregisters a keyboard shortcut registered with the given name. This will also remove any key mappings that reference this shortcut.

Parameters

ParameterTypeDescription
shortcutNamestringThe name of the shortcut to unregister.

Returns

boolean

True if an item was unregistered, false otherwise.


addKeyMapping()

addKeyMapping(
keyCode,
shortcutName,
allowCollision?
): void;

Defined in: packages/blockly/core/shortcut_registry.ts:109

Adds a mapping between a keycode and a keyboard shortcut.

Normally only one shortcut can be mapped to any given keycode, but setting allowCollisions to true allows a keyboard to be mapped to multiple shortcuts. In that case, when onKeyDown is called with the given keystroke, it will process the mapped shortcuts in reverse order, from the most- to least-recently mapped).

Parameters

ParameterTypeDescription
keyCode| string | number | CTRL_CMDThe key code for the keyboard shortcut. If registering a key code with a modifier (ex: ctrl+c) use ShortcutRegistry.registry.createSerializedKey;
shortcutNamestringThe name of the shortcut to execute when the given keycode is pressed.
allowCollision?booleanTrue to prevent an error when adding a shortcut to a key that is already mapped to a shortcut.

Returns

void

Throws

if the given key code is already mapped to a shortcut.


removeKeyMapping()

removeKeyMapping(
keyCode,
shortcutName,
quiet?
): boolean;

Defined in: packages/blockly/core/shortcut_registry.ts:141

Removes a mapping between a keycode and a keyboard shortcut.

Parameters

ParameterTypeDescription
keyCodestringThe key code for the keyboard shortcut. If registering a key code with a modifier (ex: ctrl+c) use ShortcutRegistry.registry.createSerializedKey;
shortcutNamestringThe name of the shortcut to execute when the given keycode is pressed.
quiet?booleanTrue to not console warn when there is no shortcut to remove.

Returns

boolean

True if a key mapping was removed, false otherwise.


removeAllKeyMappings()

removeAllKeyMappings(shortcutName): void;

Defined in: packages/blockly/core/shortcut_registry.ts:178

Removes all the key mappings for a shortcut with the given name. Useful when changing the default key mappings and the key codes registered to the shortcut are unknown.

Parameters

ParameterTypeDescription
shortcutNamestringThe name of the shortcut to remove from the key map.

Returns

void


setKeyMap()

setKeyMap(newKeyMap): void;

Defined in: packages/blockly/core/shortcut_registry.ts:190

Sets the key map. Setting the key map will override any default key mappings.

Parameters

ParameterTypeDescription
newKeyMap{ [key: string]: string[]; }The object with key code to shortcut names.

Returns

void


getKeyMap()

getKeyMap(): object;

Defined in: packages/blockly/core/shortcut_registry.ts:202

Gets the current key map.

Returns

object

The object holding key codes to ShortcutRegistry.KeyboardShortcut.


getRegistry()

getRegistry(): object;

Defined in: packages/blockly/core/shortcut_registry.ts:215

Gets the registry of keyboard shortcuts.

Returns

object

The registry of keyboard shortcuts.


onKeyDown()

onKeyDown(workspace, e): boolean;

Defined in: packages/blockly/core/shortcut_registry.ts:246

Handles key down events.

  • Any KeyboardShortcut(s) mapped to the keycodes that cause event e to be fired will be processed, in order from least- to most-recently registered.
  • If the shortcut's preconditionFn exists it will be called. If preconditionFn returns false the shortcut's callback function will be skipped. Processing will continue with the next shortcut, if any.
  • The shortcut's callback function will then be called. If it returns true, processing will terminate and onKeyDown will return true. If it returns false, processing will continue with with the next shortcut, if any.
  • If all registered shortcuts for the given keycode have been processed without any having returned true, onKeyDown will return false.

Parameters

ParameterTypeDescription
workspaceWorkspaceSvgThe main workspace where the event was captured.
eKeyboardEventThe key down event.

Returns

boolean

True if the event was handled, false otherwise.


getShortcutNamesByKeyCode()

getShortcutNamesByKeyCode(keyCode): string[] | undefined;

Defined in: packages/blockly/core/shortcut_registry.ts:280

Gets the shortcuts registered to the given key code.

Parameters

ParameterTypeDescription
keyCodestringThe serialized key code.

Returns

string[] | undefined

The list of shortcuts to call when the given keyCode is used. Undefined if no shortcuts exist.


getKeyCodesByShortcutName()

getKeyCodesByShortcutName(shortcutName): string[];

Defined in: packages/blockly/core/shortcut_registry.ts:294

Gets the serialized key codes that the shortcut with the given name is registered under.

Parameters

ParameterTypeDescription
shortcutNamestringThe name of the shortcut.

Returns

string[]

An array with all the key codes the shortcut is registered under.


createSerializedKey()

createSerializedKey(keyCode, modifiers): string;

Defined in: packages/blockly/core/shortcut_registry.ts:350

Creates the serialized key code that will be used in the key map.

Parameters

ParameterTypeDescription
keyCodenumberNumber code representing the key.
modifiersKeyCodes[] | nullList of modifier key codes to be used with the key. All valid modifiers can be found in the ShortcutRegistry.modifierKeys.

Returns

string

The serialized key code for the given modifiers and key.