Skip to main content

Home > blockly > ShortcutRegistry > addKeyMapping

ShortcutRegistry.addKeyMapping() method

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).

Signature:

addKeyMapping(keyCode: string | number | KeyCodes, shortcutName: string, allowCollision?: boolean): void;

Parameters

ParameterTypeDescription
keyCodestring | number | KeyCodesThe 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.
allowCollisionboolean(Optional) True to prevent an error when adding a shortcut to a key that is already mapped to a shortcut.

Returns:

void

Exceptions

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