使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
blockly > ShortcutRegistry > addKeyMapping
ShortcutRegistry.addKeyMapping() 方法
在按键代码和键盘快捷键之间添加映射。
通常,任何给定按键代码只能映射到一个快捷键,但将 allowCollisions 设为 true 后,一个键盘可以映射到多个快捷键。在这种情况下,使用给定按键调用 onKeyDown 时,它会按相反的顺序(从最近映射的到最早映射的)处理映射的快捷键。
Signature:
addKeyMapping(keyCode: string | number | KeyCodes, shortcutName: string, allowCollision?: boolean): void;
参数
参数 |
类型 |
说明 |
keyCode |
字符串 | 数字 | KeyCodes |
键盘快捷键的按键代码。如需注册带修饰符的按键代码(例如:Ctrl+C),请使用 ShortcutRegistry.registry.createSerializedKey; |
shortcutName |
字符串 |
按下指定键码时要执行的快捷键的名称。 |
allowCollision |
布尔值 |
(可选)如果为 true,则可防止向已映射到快捷键的按键添加快捷键时出现错误。 |
返回:
void
异常
如果指定的按键代码已映射到快捷方式,则返回 {Error}。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-25。
[null,null,["最后更新时间 (UTC):2025-07-25。"],[],["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"],null,[]]