键盘绑定
¥Keyboard Bindings
如果你对任何默认键盘绑定不满意,可以使用 keyboard
配置选项覆盖它们。
¥If you're unhappy with any of the default keyboard bindings you can override them using the keyboard
config option.
Reveal.configure({
keyboard: {
27: () => { console.log('esc') }, // do something custom when ESC is pressed
13: 'next', // go to the next slide when the ENTER key is pressed
32: null // don't do anything when SPACE is pressed (i.e. disable a reveal.js default binding)
}
});
键盘对象是键码及其对应操作的映射。操作可以分为三种不同的类型。
¥The keyboard object is a map of key codes and their corresponding action. The action can be of three different types.
类型 | 操作 |
---|---|
函数 | 触发回调函数。 |
字符串 | 在 reveal.js API 中调用给定的方法名称。 |
null | 禁用按键(阻止默认的 reveal.js 操作) |
通过 JavaScript 添加键盘绑定
¥Adding Keyboard Bindings via JavaScript
还可以使用 JavaScript 添加和删除自定义按键绑定。自定义按键绑定将覆盖默认键盘绑定,但反过来会被 keyboard
配置选项中用户定义的按键绑定覆盖。
¥Custom key bindings can also be added and removed using Javascript. Custom key bindings will override the default keyboard bindings, but will in turn be overridden by the user defined bindings in the keyboard
config option.
Reveal.addKeyBinding(binding, callback);
Reveal.removeKeyBinding(keyCode);
例如
¥For example
// The binding parameter provides the following properties
// keyCode: the keycode for binding to the callback
// key: the key label to show in the help overlay
// description: the description of the action to show in the help overlay
Reveal.addKeyBinding(
{ keyCode: 84, key: 'T', description: 'Start timer' },
() => {
// start timer
}
);
// The binding parameter can also be a direct keycode without providing the help description
Reveal.addKeyBinding(82, () => {
// reset timer
});
这允许插件直接向 Reveal 添加按键绑定,以便它们可以:
¥This allows plugins to add key bindings directly to Reveal so they can:
利用 Reveal 的预处理逻辑进行按键处理(例如,在暂停时忽略按键操作)
¥Make use of Reveal's pre-processing logic for key handling (for example, ignoring key presses when paused)
包含在帮助叠加层中(可选)
¥Be included in the help overlay (optional)