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
| Property | Description |
|---|---|
| registry | - |
Methods index
| Method | Description |
|---|---|
| reset | Clear and recreate the registry and keyMap. |
| register | Registers a keyboard shortcut. |
| unregister | Unregisters a keyboard shortcut registered with the given name. This will also remove any key mappings that reference this shortcut. |
| addKeyMapping | Adds a mapping between a keycode and a keyboard shortcut. |
| removeKeyMapping | Removes a mapping between a keycode and a keyboard shortcut. |
| removeAllKeyMappings | 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. |
| setKeyMap | Sets the key map. Setting the key map will override any default key mappings. |
| getKeyMap | Gets the current key map. |
| getRegistry | Gets the registry of keyboard shortcuts. |
| onKeyDown | Handles key down events. |
| getShortcutNamesByKeyCode | Gets the shortcuts registered to the given key code. |
| getKeyCodesByShortcutName | Gets the serialized key codes that the shortcut with the given name is registered under. |
| createSerializedKey | Creates 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
| Parameter | Type | Description |
|---|---|---|
shortcut | KeyboardShortcut | The shortcut for this key code. |
allowOverrides? | boolean | True 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
| Parameter | Type | Description |
|---|---|---|
shortcutName | string | The 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
| Parameter | Type | Description |
|---|---|---|
keyCode | | string | number | CTRL_CMD | The key code for the keyboard shortcut. If registering a key code with a modifier (ex: ctrl+c) use ShortcutRegistry.registry.createSerializedKey; |
shortcutName | string | The name of the shortcut to execute when the given keycode is pressed. |
allowCollision? | boolean | True 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
| Parameter | Type | Description |
|---|---|---|
keyCode | string | The key code for the keyboard shortcut. If registering a key code with a modifier (ex: ctrl+c) use ShortcutRegistry.registry.createSerializedKey; |
shortcutName | string | The name of the shortcut to execute when the given keycode is pressed. |
quiet? | boolean | True 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
| Parameter | Type | Description |
|---|---|---|
shortcutName | string | The 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
| Parameter | Type | Description |
|---|---|---|
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 eventeto be fired will be processed, in order from least- to most-recently registered. - If the shortcut's
preconditionFnexists it will be called. IfpreconditionFnreturns false the shortcut'scallbackfunction will be skipped. Processing will continue with the next shortcut, if any. - The shortcut's
callbackfunction will then be called. If it returns true, processing will terminate andonKeyDownwill 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,
onKeyDownwill return false.
Parameters
| Parameter | Type | Description |
|---|---|---|
workspace | WorkspaceSvg | The main workspace where the event was captured. |
e | KeyboardEvent | The 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
| Parameter | Type | Description |
|---|---|---|
keyCode | string | The 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
| Parameter | Type | Description |
|---|---|---|
shortcutName | string | The 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
| Parameter | Type | Description |
|---|---|---|
keyCode | number | Number code representing the key. |
modifiers | KeyCodes[] | null | List 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.