キーボードバインディング

デフォルトのキーボードバインディングのどれかに不満がある場合は、keyboard設定オプションを使用してオーバーライドできます。

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)
  }
});

キーボードオブジェクトはキーコードと対応するアクションのマップです。アクションには3つの異なるタイプがあります。

タイプアクション
関数コールバック関数をトリガーします。
文字列reveal.js APIで指定されたメソッド名を呼び出します。
nullキーを無効にする(reveal.jsのデフォルトアクションをブロックする)

JavaScriptを使用したキーボードバインディングの追加

カスタムのキーバインディングは、Javascriptを使用して追加および削除することもできます。カスタムのキーバインディングはデフォルトのキーボードバインディングをオーバーライドしますが、keyboard設定オプションでユーザー定義のバインディングによってオーバーライドされます。

Reveal.addKeyBinding(binding, callback);
Reveal.removeKeyBinding(keyCode);

例えば

// 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にキーバインディングを追加して、次のことができます。

  • Revealのキー処理に関する前処理ロジックを利用する(例:一時停止時にキー入力を無視する)
  • ヘルプオーバーレイに含める(オプション)