blockly > ShortcutRegistry > addKeyMapping
ShortcutRegistry.addKeyMapping() method
Adds a mapping between a keycode and a keyboard shortcut.
Signature:
addKeyMapping(keyCode: string | number | KeyCodes, shortcutName: string, opt_allowCollision?: boolean): void;
Parameters
Parameter |
Type |
Description |
keyCode |
string | number | KeyCodes |
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. |
opt_allowCollision |
boolean |
(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.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-09-18 UTC.
[null,null,["Last updated 2024-09-18 UTC."],[[["The `ShortcutRegistry.addKeyMapping()` method associates a key code with a keyboard shortcut's name for execution."],["It takes the key code, shortcut name, and an optional collision allowance as parameters."],["An error is thrown if the key code is already assigned unless `opt_allowCollision` is set to true."]]],["The `addKeyMapping` method in `ShortcutRegistry` establishes a link between a key code and a keyboard shortcut name. It takes the `keyCode` (string, number, or `KeyCodes` enum), the `shortcutName` (string), and an optional `opt_allowCollision` (boolean) parameter. It maps a key to a shortcut. If the key is already mapped and `opt_allowCollision` is false, it throws an error. Otherwise no error will be thrown. This method returns void.\n"]]