blob: 5666b44da288dd0655b13cd0b56cb851bdec91c4 [file] [log] [blame]
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02001import { enterFullscreen } from '../utils/util.js'
2
3/**
4 * Handles all reveal.js keyboard interactions.
5 */
6export default class Keyboard {
7
8 constructor( Reveal ) {
9
10 this.Reveal = Reveal;
11
12 // A key:value map of keyboard keys and descriptions of
13 // the actions they trigger
14 this.shortcuts = {};
15
16 // Holds custom key code mappings
17 this.bindings = {};
18
19 this.onDocumentKeyDown = this.onDocumentKeyDown.bind( this );
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +020020
21 }
22
23 /**
24 * Called when the reveal.js config is updated.
25 */
26 configure( config, oldConfig ) {
27
28 if( config.navigationMode === 'linear' ) {
29 this.shortcuts['→ , ↓ , SPACE , N , L , J'] = 'Next slide';
30 this.shortcuts['← , ↑ , P , H , K'] = 'Previous slide';
31 }
32 else {
33 this.shortcuts['N , SPACE'] = 'Next slide';
Christophe Dervieux8afae132021-12-06 15:16:42 +010034 this.shortcuts['P , Shift SPACE'] = 'Previous slide';
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +020035 this.shortcuts['← , H'] = 'Navigate left';
36 this.shortcuts['→ , L'] = 'Navigate right';
37 this.shortcuts['↑ , K'] = 'Navigate up';
38 this.shortcuts['↓ , J'] = 'Navigate down';
39 }
40
Christophe Dervieux8afae132021-12-06 15:16:42 +010041 this.shortcuts['Alt + ←/&#8593/→/↓'] = 'Navigate without fragments';
42 this.shortcuts['Shift + ←/&#8593/→/↓'] = 'Jump to first/last slide';
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +020043 this.shortcuts['B , .'] = 'Pause';
44 this.shortcuts['F'] = 'Fullscreen';
Marc Kupietz09b75752023-10-07 09:32:19 +020045 this.shortcuts['G'] = 'Jump to slide';
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +020046 this.shortcuts['ESC, O'] = 'Slide overview';
47
48 }
49
50 /**
51 * Starts listening for keyboard events.
52 */
53 bind() {
54
55 document.addEventListener( 'keydown', this.onDocumentKeyDown, false );
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +020056
57 }
58
59 /**
60 * Stops listening for keyboard events.
61 */
62 unbind() {
63
64 document.removeEventListener( 'keydown', this.onDocumentKeyDown, false );
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +020065
66 }
67
68 /**
69 * Add a custom key binding with optional description to
70 * be added to the help screen.
71 */
72 addKeyBinding( binding, callback ) {
73
74 if( typeof binding === 'object' && binding.keyCode ) {
75 this.bindings[binding.keyCode] = {
76 callback: callback,
77 key: binding.key,
78 description: binding.description
79 };
80 }
81 else {
82 this.bindings[binding] = {
83 callback: callback,
84 key: null,
85 description: null
86 };
87 }
88
89 }
90
91 /**
92 * Removes the specified custom key binding.
93 */
94 removeKeyBinding( keyCode ) {
95
96 delete this.bindings[keyCode];
97
98 }
99
100 /**
101 * Programmatically triggers a keyboard event
102 *
103 * @param {int} keyCode
104 */
105 triggerKey( keyCode ) {
106
107 this.onDocumentKeyDown( { keyCode } );
108
109 }
110
111 /**
112 * Registers a new shortcut to include in the help overlay
113 *
114 * @param {String} key
115 * @param {String} value
116 */
117 registerKeyboardShortcut( key, value ) {
118
119 this.shortcuts[key] = value;
120
121 }
122
123 getShortcuts() {
124
125 return this.shortcuts;
126
127 }
128
129 getBindings() {
130
131 return this.bindings;
132
133 }
134
135 /**
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200136 * Handler for the document level 'keydown' event.
137 *
138 * @param {object} event
139 */
140 onDocumentKeyDown( event ) {
141
142 let config = this.Reveal.getConfig();
143
144 // If there's a condition specified and it returns false,
145 // ignore this event
146 if( typeof config.keyboardCondition === 'function' && config.keyboardCondition(event) === false ) {
147 return true;
148 }
149
150 // If keyboardCondition is set, only capture keyboard events
151 // for embedded decks when they are focused
152 if( config.keyboardCondition === 'focused' && !this.Reveal.isFocused() ) {
153 return true;
154 }
155
156 // Shorthand
157 let keyCode = event.keyCode;
158
159 // Remember if auto-sliding was paused so we can toggle it
160 let autoSlideWasPaused = !this.Reveal.isAutoSliding();
161
162 this.Reveal.onUserInput( event );
163
164 // Is there a focused element that could be using the keyboard?
165 let activeElementIsCE = document.activeElement && document.activeElement.isContentEditable === true;
166 let activeElementIsInput = document.activeElement && document.activeElement.tagName && /input|textarea/i.test( document.activeElement.tagName );
167 let activeElementIsNotes = document.activeElement && document.activeElement.className && /speaker-notes/i.test( document.activeElement.className);
168
Christophe Dervieux8afae132021-12-06 15:16:42 +0100169 // Whitelist certain modifiers for slide navigation shortcuts
Marc Kupietz9c036a42024-05-14 13:17:25 +0200170 let keyCodeUsesModifier = [32, 37, 38, 39, 40, 63, 78, 80, 191].indexOf( event.keyCode ) !== -1;
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200171
172 // Prevent all other events when a modifier is pressed
Marc Kupietz9c036a42024-05-14 13:17:25 +0200173 let unusedModifier = !( keyCodeUsesModifier && event.shiftKey || event.altKey ) &&
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200174 ( event.shiftKey || event.altKey || event.ctrlKey || event.metaKey );
175
176 // Disregard the event if there's a focused element or a
177 // keyboard modifier key is present
178 if( activeElementIsCE || activeElementIsInput || activeElementIsNotes || unusedModifier ) return;
179
180 // While paused only allow resume keyboard events; 'b', 'v', '.'
Marc Kupietz9c036a42024-05-14 13:17:25 +0200181 let resumeKeyCodes = [66,86,190,191,112];
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200182 let key;
183
184 // Custom key bindings for togglePause should be able to resume
185 if( typeof config.keyboard === 'object' ) {
186 for( key in config.keyboard ) {
187 if( config.keyboard[key] === 'togglePause' ) {
188 resumeKeyCodes.push( parseInt( key, 10 ) );
189 }
190 }
191 }
192
193 if( this.Reveal.isPaused() && resumeKeyCodes.indexOf( keyCode ) === -1 ) {
194 return false;
195 }
196
197 // Use linear navigation if we're configured to OR if
198 // the presentation is one-dimensional
199 let useLinearMode = config.navigationMode === 'linear' || !this.Reveal.hasHorizontalSlides() || !this.Reveal.hasVerticalSlides();
200
201 let triggered = false;
202
203 // 1. User defined key bindings
204 if( typeof config.keyboard === 'object' ) {
205
206 for( key in config.keyboard ) {
207
208 // Check if this binding matches the pressed key
209 if( parseInt( key, 10 ) === keyCode ) {
210
211 let value = config.keyboard[ key ];
212
213 // Callback function
214 if( typeof value === 'function' ) {
215 value.apply( null, [ event ] );
216 }
217 // String shortcuts to reveal.js API
218 else if( typeof value === 'string' && typeof this.Reveal[ value ] === 'function' ) {
219 this.Reveal[ value ].call();
220 }
221
222 triggered = true;
223
224 }
225
226 }
227
228 }
229
230 // 2. Registered custom key bindings
231 if( triggered === false ) {
232
233 for( key in this.bindings ) {
234
235 // Check if this binding matches the pressed key
236 if( parseInt( key, 10 ) === keyCode ) {
237
238 let action = this.bindings[ key ].callback;
239
240 // Callback function
241 if( typeof action === 'function' ) {
242 action.apply( null, [ event ] );
243 }
244 // String shortcuts to reveal.js API
245 else if( typeof action === 'string' && typeof this.Reveal[ action ] === 'function' ) {
246 this.Reveal[ action ].call();
247 }
248
249 triggered = true;
250 }
251 }
252 }
253
254 // 3. System defined key bindings
255 if( triggered === false ) {
256
257 // Assume true and try to prove false
258 triggered = true;
259
260 // P, PAGE UP
261 if( keyCode === 80 || keyCode === 33 ) {
Christophe Dervieux8afae132021-12-06 15:16:42 +0100262 this.Reveal.prev({skipFragments: event.altKey});
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200263 }
264 // N, PAGE DOWN
265 else if( keyCode === 78 || keyCode === 34 ) {
Christophe Dervieux8afae132021-12-06 15:16:42 +0100266 this.Reveal.next({skipFragments: event.altKey});
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200267 }
268 // H, LEFT
269 else if( keyCode === 72 || keyCode === 37 ) {
Christophe Dervieux8afae132021-12-06 15:16:42 +0100270 if( event.shiftKey ) {
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200271 this.Reveal.slide( 0 );
272 }
273 else if( !this.Reveal.overview.isActive() && useLinearMode ) {
Marc Kupietz9c036a42024-05-14 13:17:25 +0200274 if( config.rtl ) {
275 this.Reveal.next({skipFragments: event.altKey});
276 }
277 else {
278 this.Reveal.prev({skipFragments: event.altKey});
279 }
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200280 }
281 else {
Christophe Dervieux8afae132021-12-06 15:16:42 +0100282 this.Reveal.left({skipFragments: event.altKey});
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200283 }
284 }
285 // L, RIGHT
286 else if( keyCode === 76 || keyCode === 39 ) {
Christophe Dervieux8afae132021-12-06 15:16:42 +0100287 if( event.shiftKey ) {
288 this.Reveal.slide( this.Reveal.getHorizontalSlides().length - 1 );
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200289 }
290 else if( !this.Reveal.overview.isActive() && useLinearMode ) {
Marc Kupietz9c036a42024-05-14 13:17:25 +0200291 if( config.rtl ) {
292 this.Reveal.prev({skipFragments: event.altKey});
293 }
294 else {
295 this.Reveal.next({skipFragments: event.altKey});
296 }
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200297 }
298 else {
Christophe Dervieux8afae132021-12-06 15:16:42 +0100299 this.Reveal.right({skipFragments: event.altKey});
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200300 }
301 }
302 // K, UP
303 else if( keyCode === 75 || keyCode === 38 ) {
Christophe Dervieux8afae132021-12-06 15:16:42 +0100304 if( event.shiftKey ) {
305 this.Reveal.slide( undefined, 0 );
306 }
307 else if( !this.Reveal.overview.isActive() && useLinearMode ) {
308 this.Reveal.prev({skipFragments: event.altKey});
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200309 }
310 else {
Christophe Dervieux8afae132021-12-06 15:16:42 +0100311 this.Reveal.up({skipFragments: event.altKey});
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200312 }
313 }
314 // J, DOWN
315 else if( keyCode === 74 || keyCode === 40 ) {
Christophe Dervieux8afae132021-12-06 15:16:42 +0100316 if( event.shiftKey ) {
317 this.Reveal.slide( undefined, Number.MAX_VALUE );
318 }
319 else if( !this.Reveal.overview.isActive() && useLinearMode ) {
320 this.Reveal.next({skipFragments: event.altKey});
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200321 }
322 else {
Christophe Dervieux8afae132021-12-06 15:16:42 +0100323 this.Reveal.down({skipFragments: event.altKey});
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200324 }
325 }
326 // HOME
327 else if( keyCode === 36 ) {
328 this.Reveal.slide( 0 );
329 }
330 // END
331 else if( keyCode === 35 ) {
Christophe Dervieux8afae132021-12-06 15:16:42 +0100332 this.Reveal.slide( this.Reveal.getHorizontalSlides().length - 1 );
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200333 }
334 // SPACE
335 else if( keyCode === 32 ) {
336 if( this.Reveal.overview.isActive() ) {
337 this.Reveal.overview.deactivate();
338 }
339 if( event.shiftKey ) {
Christophe Dervieux8afae132021-12-06 15:16:42 +0100340 this.Reveal.prev({skipFragments: event.altKey});
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200341 }
342 else {
Christophe Dervieux8afae132021-12-06 15:16:42 +0100343 this.Reveal.next({skipFragments: event.altKey});
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200344 }
345 }
346 // TWO-SPOT, SEMICOLON, B, V, PERIOD, LOGITECH PRESENTER TOOLS "BLACK SCREEN" BUTTON
Marc Kupietz9c036a42024-05-14 13:17:25 +0200347 else if( [58, 59, 66, 86, 190].includes( keyCode ) || ( keyCode === 191 && !event.shiftKey ) ) {
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200348 this.Reveal.togglePause();
349 }
350 // F
351 else if( keyCode === 70 ) {
352 enterFullscreen( config.embedded ? this.Reveal.getViewportElement() : document.documentElement );
353 }
354 // A
355 else if( keyCode === 65 ) {
Marc Kupietz9c036a42024-05-14 13:17:25 +0200356 if( config.autoSlideStoppable ) {
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200357 this.Reveal.toggleAutoSlide( autoSlideWasPaused );
358 }
359 }
Marc Kupietz09b75752023-10-07 09:32:19 +0200360 // G
361 else if( keyCode === 71 ) {
Marc Kupietz9c036a42024-05-14 13:17:25 +0200362 if( config.jumpToSlide ) {
Marc Kupietz09b75752023-10-07 09:32:19 +0200363 this.Reveal.toggleJumpToSlide();
364 }
365 }
Marc Kupietz9c036a42024-05-14 13:17:25 +0200366 // ?
367 else if( ( keyCode === 63 || keyCode === 191 ) && event.shiftKey ) {
368 this.Reveal.toggleHelp();
369 }
370 // F1
371 else if( keyCode === 112 ) {
372 this.Reveal.toggleHelp();
373 }
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200374 else {
375 triggered = false;
376 }
377
378 }
379
380 // If the input resulted in a triggered action we should prevent
381 // the browsers default behavior
382 if( triggered ) {
383 event.preventDefault && event.preventDefault();
384 }
385 // ESC or O key
386 else if( keyCode === 27 || keyCode === 79 ) {
387 if( this.Reveal.closeOverlay() === false ) {
388 this.Reveal.overview.toggle();
389 }
390
391 event.preventDefault && event.preventDefault();
392 }
393
394 // If auto-sliding is enabled we need to cue up
395 // another timeout
396 this.Reveal.cueAutoSlide();
397
398 }
399
Marc Kupietz9c036a42024-05-14 13:17:25 +0200400}