summaryrefslogtreecommitdiffstats
path: root/browser/src/map/handler/Map.Keyboard.js
blob: 96991a57cc818f62e6960d6f03c7ac28efc6cabc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
/* -*- js-indent-level: 8 -*- */
/*
 * L.Map.Keyboard is handling keyboard interaction with the map, enabled by default.
 *
 * It handles keyboard interactions which are NOT text input, including those which
 * don't require edit permissions (e.g. page scroll). Text input is handled
 * at TextInput.
 */

/* global app UNOKey UNOModifier */

L.Map.mergeOptions({
	keyboard: true,
	keyboardPanOffset: 20,
	keyboardZoomOffset: 1
});

L.Map.Keyboard = L.Handler.extend({

	keymap: {
		8   : UNOKey.BACKSPACE,
		9   : UNOKey.TAB,
		13  : UNOKey.RETURN,
		16  : null, // shift		: UNKOWN
		17  : null, // ctrl		: UNKOWN
		18  : null, // alt		: UNKOWN
		19  : null, // pause/break	: UNKOWN
		20  : null, // caps lock	: UNKOWN
		27  : UNOKey.ESCAPE,
		32  : UNOKey.SPACE,
		33  : UNOKey.PAGEUP,
		34  : UNOKey.PAGEDOWN,
		35  : UNOKey.END,
		36  : UNOKey.HOME,
		37  : UNOKey.LEFT,
		38  : UNOKey.UP,
		39  : UNOKey.RIGHT,
		40  : UNOKey.DOWN,
		45  : UNOKey.INSERT,
		46  : UNOKey.DELETE,
		48  : UNOKey.NUM0,
		49  : UNOKey.NUM1,
		50  : UNOKey.NUM2,
		51  : UNOKey.NUM3,
		52  : UNOKey.NUM4,
		53  : UNOKey.NUM5,
		54  : UNOKey.NUM6,
		55  : UNOKey.NUM7,
		56  : UNOKey.NUM8,
		57  : UNOKey.NUM9,
		65  : UNOKey.A,
		66  : UNOKey.B,
		67  : UNOKey.C,
		68  : UNOKey.D,
		69  : UNOKey.E,
		70  : UNOKey.F,
		71  : UNOKey.G,
		72  : UNOKey.H,
		73  : UNOKey.I,
		74  : UNOKey.J,
		75  : UNOKey.K,
		76  : UNOKey.L,
		77  : UNOKey.M,
		78  : UNOKey.N,
		79  : UNOKey.O,
		80  : UNOKey.P,
		81  : UNOKey.Q,
		82  : UNOKey.R,
		83  : UNOKey.S,
		84  : UNOKey.T,
		85  : UNOKey.U,
		86  : UNOKey.V,
		87  : UNOKey.W,
		88  : UNOKey.X,
		89  : UNOKey.Y,
		90  : UNOKey.Z,
		91  : null, // left window key	: UNKOWN
		92  : null, // right window key	: UNKOWN
		93  : null, // select key	: UNKOWN
		96  : UNOKey.NUM0,
		97  : UNOKey.NUM1,
		98  : UNOKey.NUM2,
		99  : UNOKey.NUM3,
		100 : UNOKey.NUM4,
		101 : UNOKey.NUM5,
		102 : UNOKey.NUM6,
		103 : UNOKey.NUM7,
		104 : UNOKey.NUM8,
		105 : UNOKey.NUM9,
		106 : UNOKey.MULTIPLY,
		107 : UNOKey.ADD,
		109 : UNOKey.SUBTRACT,
		110 : UNOKey.DECIMAL,
		111 : UNOKey.DIVIDE,
		112 : UNOKey.F1,
		113 : UNOKey.F2,
		114 : UNOKey.F3,
		115 : UNOKey.F4,
		116 : UNOKey.F5,
		117 : UNOKey.F6,
		118 : UNOKey.F7,
		119 : UNOKey.F8,
		120 : UNOKey.F9,
		121 : UNOKey.F10,
		122 : UNOKey.F11,
		123 : UNOKey.F12,
		144 : UNOKey.NUMLOCK,
		145 : UNOKey.SCROLLLOCK,
		173 : UNOKey.SUBTRACT,
		186 : UNOKey.SEMICOLON,
		187 : UNOKey.EQUAL,
		188 : UNOKey.COMMA,
		189 : UNOKey.SUBTRACT,
		190 : null, // period		: UNKOWN
		191 : null, // forward slash	: UNKOWN
		192 : null, // grave accent	: UNKOWN
		219 : null, // open bracket	: UNKOWN
		220 : null, // back slash	: UNKOWN
		221 : null, // close bracket	: UNKOWN
		222 : null  // single quote	: UNKOWN
	},

	handleOnKeyDownKeys: {
		// these keys need to be handled on keydown in order for them
		// to work on chrome
		// Backspace and Delete are handled at TextInput's 'beforeinput' handler.
		9   : true, // tab
		19  : true, // pause/break
		20  : true, // caps lock
		27  : true, // escape
		33  : true, // page up
		34  : true, // page down
		35  : true, // end
		36  : true, // home
		37  : true, // left arrow
		38  : true, // up arrow
		39  : true, // right arrow
		40  : true, // down arrow
		45  : true, // insert
		113 : true  // f2
	},

	keyCodes: {

		pageUp:   33,
		pageDown: 34,
		enter:    13,
		BACKSPACE:8,
		TAB:      9,
		SPACE :   32,
		SHIFT:    16, // shift		: UNKOWN
		CTRL:     17, // ctrl		: UNKOWN
		ALT:      18, // alt		: UNKOWN
		PAUSE:    19, // pause/break	: UNKOWN
		CAPSLOCK: 20, // caps lock	: UNKOWN,
		PAGEUP:   33,
		PAGEDOWN: 34,
		END:      35,
		HOME:     36,
		LEFT:     37,
		UP:       38,
		RIGHT:    39,
		DOWN:     40,
		INSERT:   45,
		DELETE:   46,
		NUM0:     [48,96], // two values because of different mapping in mac and windows for same keys
		NUM1:     [49,97],
		NUM2:     [50,98],
		NUM3:     [51,99],
		NUM4:     [52,100],
		NUM5:     [53,101],
		NUM6:     [54,102],
		NUM7:     [55,103],
		NUM8:     [56,103],
		NUM9:     [57,104],
		A:        65,
		B:        66,
		C:        [67,99],
		//c:        99,
		D:        68,
		E:        69,
		//f:        70, no need for separate as for Windows it will remain the same
		F:        70,
		G:        71,
		H:        72,
		I:        73,
		J:        74,
		K:        75,
		L:        76,
		M:        77,
		N:        78,
		O:        79,
		P:        80,
		Q:        81,
		R:        82,
		S:        83,
		T:        84,
		U:        85,
		V:        [86,118],
		//v:        118, // this is for MAC as the value of v changes when we press keydown
		W:        87,
		X:        [88,120],
		//x:        120, // this is for MAC as the value of x changes when we press keydown
		Y:        89,
		Z:        90,
		LEFTWINDOWKEY :    [91,91], // left window key	: UNKOWN  and also for MAC
		RIGHTWINDOWKEY:    [92,93], // right window key	: UNKOWN  and also for MAC
		SELECTKEY:         93, // select key	: UNKOWN
		// NUM0:     96,
		// NUM1:     97,
		// NUM2:     98,
		// NUM3:     99,
		// NUM4:     100,
		// NUM5:     101,
		// NUM6:     102,
		// NUM7:     103,
		// NUM8:     104,
		MULTIPLY:    106,
		ADD:         107,
		//SUBTRACT:    109,
		DECIMAL:     110,
		DIVIDE:      111,
		F1:       112,
		F2:       113,
		F3:       114,
		F4:       115,
		F5:       116,
		F6:       117,
		F7:       118,
		F8:       119,
		F9:       120,
		F10:      121,
		F11:      122,
		F12 :     123,
		NUMLOCK:  144,
		SCROLLLOCK:   145,
		SUBTRACT:     [109,173,189],
		SEMICOLON:    186,
		EQUAL:        187,
		COMMA:        188,
		//SUBTRACT:     189,
		PERIOD:       190, // period		: UNKOWN
		FORWARDSLASH: 191, // forward slash	: UNKOWN
		GRAVEACCENT:  192, // grave accent	: UNKOWN
		OPENBRACKET:  219, // open bracket	: UNKOWN
		BACKSLASH:    220, // back slash	: UNKOWN
		CLOSEBRACKET: 221, // close bracket	: UNKOWN
		SINGLEQUOTE : 222  // single quote	: UNKOWN

	},

	navigationKeyCodes: {
		left:    [37],
		right:   [39],
		down:    [40],
		up:      [38],
		zoomIn:  [187, 107, 61, 171],
		zoomOut: [189, 109, 173]
	},

	allowedKeyCodeWhenNotEditing: {
		27:    true,  // ESC
		33:    true,  // pageUp
		34:    true,  // pageDown
		13:    true,  // enter
		 8:    true,  // BACKSPACE
		16:    true,  // SHIFT
		17:    true,  // CTRL
		18:    true,  // ALT
		19:    true,  // PAUSE
		20:    true,  // CAPSLOCK
		35:    true,  // END
		36:    true,  // HOME
		45:    true,  // INSERT
		46:    true,  // DELETE
		91:    true,  // LEFTWINDOWKEY
		92:    true,  // RIGHTWINDOWKEY
		112:    true,  //F1
		113:    true,  //F2
		114:    true,  //F3
		115:    true,  //F4
		116:    true,  //F5
		117:    true,  //F6
		118:    true,  //F7
		119:    true,  //F8
		120:    true,  //F9
		121:    true,  //F10
		122:    true,  //F11
		123:    true,  //F12
		144:    true,  //NUMLOCK
		145:    true,  //SCROLLLOCK
	},

	initialize: function (map) {
		this._map = map;
		this._setPanOffset(map.options.keyboardPanOffset);
		this._setZoomOffset(map.options.keyboardZoomOffset);
		this.modifier = 0;
		window.KeyboardShortcuts.initialize(map);
	},


	addHooks: function () {
		var container = this._map._container;

		// make the container focusable by tabbing
		if (container.tabIndex === -1) {
			container.tabIndex = '0';
		}

		L.DomEvent.on(this._map.getContainer(), 'keydown keyup keypress', this._onKeyDown, this);
		L.DomEvent.on(window.document, 'keydown', this._globalKeyEvent, this);
	},

	removeHooks: function () {
		L.DomEvent.off(this._map.getContainer(), 'keydown keyup keypress', this._onKeyDown, this);
		L.DomEvent.off(window.document, 'keydown', this._globalKeyEvent, this);
	},

	_ignoreKeyEvent: function(ev) {
		var shift = ev.shiftKey ? UNOModifier.SHIFT : 0;
		if (shift && (ev.keyCode === this.keyCodes.INSERT || ev.keyCode === this.keyCodes.DELETE)) {
			// don't handle shift+insert, shift+delete
			// These are converted to 'cut', 'paste' events which are
			// automatically handled by us, so avoid double-handling
			return true;
		}
	},

	_setPanOffset: function (pan) {
		var keys = {},
		    codes = this.navigationKeyCodes,
		    i, len;

		for (i = 0, len = codes.left.length; i < len; i++) {
			keys[codes.left[i]] = [-1 * pan, 0];
		}
		for (i = 0, len = codes.right.length; i < len; i++) {
			keys[codes.right[i]] = [pan, 0];
		}
		for (i = 0, len = codes.down.length; i < len; i++) {
			keys[codes.down[i]] = [0, pan];
		}
		for (i = 0, len = codes.up.length; i < len; i++) {
			keys[codes.up[i]] = [0, -1 * pan];
		}
	},

	_setZoomOffset: function (zoom) {
		var keys = this._zoomKeys = {},
		    codes = this.navigationKeyCodes,
		    i, len;

		for (i = 0, len = codes.zoomIn.length; i < len; i++) {
			keys[codes.zoomIn[i]] = zoom;
		}
		for (i = 0, len = codes.zoomOut.length; i < len; i++) {
			keys[codes.zoomOut[i]] = -zoom;
		}
	},

	// Convert javascript key codes to UNO key codes.
	_toUNOKeyCode: function (keyCode) {
		return this.keymap[keyCode] || keyCode;
	},

	// _onKeyDown - called only as a DOM event handler
	// Calls _handleKeyEvent(), but only if the event doesn't have
	// a charCode property (set to something different than 0) - that ignores
	// any 'beforeinput', 'keypress' and 'input' events that would add
	// printable characters. Those are handled by TextInput.js.
	_onKeyDown: function (ev) {
		if (this._map.uiManager.isUIBlocked()
			|| ((this._map._docLayer._docType === 'presentation' || this._map._docLayer._docType === 'drawing') && this._map._docLayer._preview.partsFocused === true)
		)
			return;

		if (this._map._debug.logKeyboardEvents) {
			window.app.console.log('keyboard handler:', ev.type, ev.key, ev.charCode, this._expectingInput, ev);
		}

		var completeEvent = app.socket.createCompleteTraceEvent('L.Map.Keyboard._onKeyDown', { type: ev.type, charCode: ev.charCode });

		if (ev.charCode == 0) {
			this._handleKeyEvent(ev);
		}
		if (this._map._docLayer)
			if (ev.shiftKey && ev.type === 'keydown')
				this._map._docLayer.shiftKeyPressed = true;
			else if (ev.keyCode === this.keyCodes.SHIFT && ev.type === 'keyup')
				this._map._docLayer.shiftKeyPressed = false;
		if (completeEvent)
			completeEvent.finish();
	},

	_globalKeyEvent: function(ev) {
		if (this._map.uiManager.isUIBlocked())
			return;

		if (this._map.jsdialog
			&& (this._map.jsdialog.hasDialogOpened() || this._map.jsdialog.hasSnackbarOpened() || this._map.jsdialog.hasDropdownOpened())
			&& this._map.jsdialog.handleKeyEvent(ev)) {
			ev.preventDefault();
			return;
		}
		else if (this._map._docLayer && (this._map._docLayer._docType === 'presentation' || this._map._docLayer._docType === 'drawing') && this._map._docLayer._preview.partsFocused === true) {

			if (!this.modifier && (ev.keyCode === this.keyCodes.DOWN || ev.keyCode === this.keyCodes.UP ||
				               ev.keyCode === this.keyCodes.RIGHT || ev.keyCode === this.keyCodes.LEFT ||
				               ev.keyCode === this.keyCodes.PAGEDOWN || ev.keyCode === this.keyCodes.PAGEUP ||
				               ev.keyCode === this.keyCodes.DELETE || ev.keyCode === this.keyCodes.BACKSPACE)
				           && ev.type === 'keydown') {

				var deletePart = (ev.keyCode === this.keyCodes.DELETE || ev.keyCode === this.keyCodes.BACKSPACE) ? true : false;

				if (!deletePart) {
					var partToSelect = (ev.keyCode === this.keyCodes.UP || ev.keyCode === this.keyCodes.LEFT ||
						            ev.keyCode === this.keyCodes.PAGEUP) ? 'prev' : 'next';
					this._map.setPart(partToSelect);
					if (app.file.fileBasedView)
						this._map._docLayer._checkSelectedPart();
				}
				else if (this._map.isEditMode() && !app.file.fileBasedView) {
					this._map.deletePage(this._map._docLayer._selectedPart);
				}
				ev.preventDefault();
				return;
			}
			else if (ev.ctrlKey) {
				if (!ev.altKey && ev.keyCode === this.keyCodes.HOME)
					this._map.setPart(0);
				else if (!ev.altKey && ev.keyCode === this.keyCodes.END)
					this._map.setPart(this._map._docLayer._parts - 1);
			}
			else {
				this._map._docLayer._preview.partsFocused = false;
			}
		}
	},

	// _handleKeyEvent - checks if the given keyboard event shall trigger
	// a message to coolwsd, and calls the given keyEventFn(type, charcode, keycode)
	// callback if so.
	// Called from _onKeyDown
	_handleKeyEvent: function (ev, keyEventFn) {
		if (this._map.uiManager.isUIBlocked())
			return;

		app.idleHandler.notifyActive();
		if (this._map.slideShow && this._map.slideShow.fullscreen) {
			return;
		}

		if (window.KeyboardShortcuts.processEvent(app.UI.language.fromURL, ev)) {
			ev.preventDefault();
			return;
		}

		var docLayer = this._map._docLayer;
		if (!keyEventFn && docLayer.postKeyboardEvent) {
			// default is to post keyboard events on the document
			keyEventFn = L.bind(docLayer.postKeyboardEvent, docLayer);
		}

		this.modifier = 0;
		var shift = ev.shiftKey ? UNOModifier.SHIFT : 0;
		var ctrl = (ev.ctrlKey || ev.metaKey) ? UNOModifier.CTRL : 0;
		var alt = ev.altKey ? UNOModifier.ALT : 0;
		var location = ev.location;
		this.modifier = shift | ctrl | alt;

		// On Windows, pressing AltGr = Alt + Ctrl
		// Presence of AltGr is detected if previous Ctrl + Alt 'location' === 2 (i.e right)
		// because Ctrl + Alt + <some char> won't give any 'location' information.
		if (ctrl && alt) {
			if (ev.type === 'keydown' && location === 2) {
				this._prevCtrlAltLocation = location;
				return;
			}
			else if (location === 1) {
				this._prevCtrlAltLocation = undefined;
			}

			if (this._prevCtrlAltLocation === 2 && location === 0) {
				// and we got the final character
				if (ev.type === 'keypress') {
					ctrl = alt = this.modifier = 0;
				}
				else {
					// Don't handle remnant 'keyup'
					return;
				}
			}
		}

		if (ctrl) {
			if (this._handleCtrlCommand(ev)) {
				return;
			}
		}

		var charCode = ev.charCode;
		var keyCode = ev.keyCode;

		var DEFAULT =0;
		//var MAC=1; use this when you encounter a MAC value

		if ((this.modifier == UNOModifier.ALT || this.modifier == UNOModifier.SHIFT + UNOModifier.ALT) &&
		    keyCode >= this.keyCodes.NUM0[DEFAULT]) {
			// Presumably a Mac or iOS client accessing a "special character". Just ignore the alt modifier.
			// But don't ignore it for Alt + non-printing keys.
			this.modifier -= alt;
			alt = 0;
		}

		var unoKeyCode = this._toUNOKeyCode(keyCode);

		if (this.modifier) {
			unoKeyCode |= this.modifier;
			if (ev.type !== 'keyup' && (this.modifier !== shift || (keyCode === this.keyCodes.SPACE && !this._map._isCursorVisible))) {
				if (keyEventFn) {
					keyEventFn('input', charCode, unoKeyCode);
					ev.preventDefault();
				}

				return;
			}
		}

		if (this._map.stateChangeHandler._items['.uno:SlideMasterPage'] === 'true') {
			ev.preventDefault();
			return;
		}

		if (this._map.isEditMode()) {
			docLayer._resetPreFetching();

			if (this._ignoreKeyEvent(ev)) {
				// key ignored
			}
			else if (ev.type === 'keydown') {
				if (this.handleOnKeyDownKeys[keyCode] && charCode === 0) {
					if (keyEventFn) {
						keyEventFn('input', charCode, unoKeyCode);
						if (!(this._map._textInput.hasAccessibilitySupport() && this._map._textInput.hasFocus() &&
							(keyCode === this.keyCodes.LEFT || keyCode === this.keyCodes.RIGHT))) {
							ev.preventDefault();
						} else {
							window.app.console.log('Map.Keyboard._handleKeyEvent: default not prevented for left or right arrow.');
						}
					}
				}
				if (this._map._debug.tileInvalidationsOn) {
					this._map._debug.addTileInvalidationKeypress();
				}
			}
			else if ((ev.type === 'keypress') && (!this.handleOnKeyDownKeys[keyCode] || charCode !== 0)) {
				if (keyCode === this.keyCodes.BACKSPACE || keyCode === this.keyCodes.DELETE || keyCode === this.keyCodes.enter)
				{
					// handled generically in TextInput.js
					window.app.console.log('Ignore backspace/delete/enter keypress');
					return;
				}
				if (charCode === keyCode && charCode !== this.keyCodes.enter) {
					// Chrome sets keyCode = charCode for printable keys
					// while LO requires it to be 0
					keyCode = 0;
					unoKeyCode = this._toUNOKeyCode(keyCode);
				}
				if (this._map._debug.tileInvalidationsOn) {
					this._map._debug.addTileInvalidationKeypress();
				}

				if (keyEventFn) {
					keyEventFn('input', charCode, unoKeyCode);
				}
			}
			else if (ev.type === 'keyup') {
				if ((this.handleOnKeyDownKeys[keyCode] && charCode === 0) ||
				    (this.modifier) ||
				    unoKeyCode === UNOKey.RETURN) {
					if (keyEventFn) {
						keyEventFn('up', charCode, unoKeyCode);
					}
				} else {
					// was handled as textinput
				}
			}
			if (keyCode === this.keyCodes.TAB) {
				// tab would change focus to other DOM elements
				ev.preventDefault();
			}
		}
		else if (ev.type === 'keydown') {
			var key = ev.keyCode;
			var map = this._map;
			if (['ArrowLeft', 'ArrowRight', 'ArrowUp', 'ArrowDown', 'Home', 'End'].includes(ev.code)) {
				keyEventFn('input', charCode, unoKeyCode);
			}
			else if (key in this._zoomKeys) {
				map.setZoom(map.getZoom() + (ev.shiftKey ? 3 : 1) * this._zoomKeys[key], null, true /* animate? */);
			}
		}

		// if any key is pressed, we stop the following other user feature.
		if (this._map._docLayer._followUser) {
			this._map.userList.followUser(this._map._docLayer._viewId);
		}


		L.DomEvent.stopPropagation(ev);
	},

	_isCtrlKey: function (e) {
		if (window.ThisIsTheiOSApp || L.Browser.mac)
			return e.metaKey;
		else
			return e.ctrlKey;
	},

	// Given a DOM keyboard event that happened while the Control key was depressed,
	// triggers the appropriate action or coolwsd message.
	_handleCtrlCommand: function (e) {

		var DEFAULT =0;
		var MAC=1;
		if (this._map.uiManager.isUIBlocked())
			return;

		// Control
		if (e.keyCode == this.keyCodes.CTRL)
			return true;

		if (e.type !== 'keydown' && e.keyCode !== this.keyCodes.C[DEFAULT] && e.keyCode !== this.keyCodes.V[DEFAULT] && e.keyCode !== this.keyCodes.X[DEFAULT] &&
		/* Safari */ e.keyCode !== this.keyCodes.C[MAC] && e.keyCode !== this.keyCodes.V[MAC] && e.keyCode !== this.keyCodes.X[MAC]) {
			e.preventDefault();
			return true;
		}

		if (e.keyCode !== this.keyCodes.C[DEFAULT] && e.keyCode !== this.keyCodes.V[DEFAULT] && e.keyCode !== this.keyCodes.X[DEFAULT] &&
		/* Safari */ e.keyCode !== this.keyCodes.C[MAC] && e.keyCode !== this.keyCodes.V[MAC] && e.keyCode !== this.keyCodes.X[MAC]) {
			// not copy or paste
			e.preventDefault();
		}

		// CTRL + ALT + N for new document. This needs to be handled by the integrator.
		if (this._isCtrlKey(e) && e.altKey && e.keyCode === this.keyCodes.N) {
			this._map.fire('postMessage', {msgId: 'UI_CreateFile', args: { DocumentType: this._map.getDocType() }});
			e.preventDefault();
			return true;
		}

		// CTRL + SHIFT + L is added to the core side for writer. Others can also be checked.
		if (this._isCtrlKey(e) && e.shiftKey && e.keyCode === this.keyCodes.L && this._map.getDocType() !== 'text' && this._map.getDocType() !== 'spreadsheet') {
			app.socket.sendMessage('uno .uno:DefaultBullet');
			e.preventDefault();
			return true;
		}

		// CTRL + ALT + O to open a document. This needs to be handled by the integrator.
		if (this._isCtrlKey(e) && e.altKey && e.keyCode === this.keyCodes.O) {
			this._map.fire('postMessage', { msgId: 'UI_OpenDocument' });
			e.preventDefault();
			return true;
		}

		if (this._isCtrlKey(e) && e.shiftKey && e.key === '?') {
			this._map.showHelp('keyboard-shortcuts-content');
			e.preventDefault();
			return true;
		}

		// Handles paste special. The "Your browser" thing seems to indicate that this code
		// snippet is relevant in a browser only.
		if (!window.ThisIsAMobileApp && e.ctrlKey && e.shiftKey && e.altKey && this.keyCodes.V.includes(e.keyCode)) {
			this._map._clip._openPasteSpecialPopup();
			return true;
		}

		// Handles unformatted paste
		if (this._isCtrlKey(e) && e.shiftKey && this.keyCodes.V.includes(e.keyCode)) {
			return true;
		}

		// Collapse / expand notebookbar.
		if (this._isCtrlKey(e) && e.keyCode === this.keyCodes.F1) {
			if (this._map.uiManager.getCurrentMode() === 'notebookbar') {
				if (this._map.uiManager.isNotebookbarCollapsed())
					this._map.uiManager.extendNotebookbar();
				else
					this._map.uiManager.collapseNotebookbar();
			}
			return true;
		}

		if (this._isCtrlKey(e) && !e.shiftKey && e.keyCode === this.keyCodes.K) {
			this._map.showHyperlinkDialog();
			e.preventDefault();
			return true;
		}

		if (this._isCtrlKey(e) && e.keyCode === this.keyCodes.Z) {
			app.socket.sendMessage('uno .uno:Undo');
			e.preventDefault();
			return true;
		}

		if (this._isCtrlKey(e) && e.keyCode === this.keyCodes.Y) {
			app.socket.sendMessage('uno .uno:Redo');
			e.preventDefault();
			return true;
		}

		if (this._isCtrlKey(e) && !e.shiftKey && !e.altKey && e.keyCode === this.keyCodes.F) {
			if (app.UI.language.fromURL === 'de' && this._map.getDocType() === 'text') {
				this._map.sendUnoCommand('.uno:Navigator');
			}
			else {
				if (!this._map.uiManager.isStatusBarVisible()) {
					this._map.uiManager.showStatusBar();
				}
				this._map.fire('focussearch');
			}

			e.preventDefault();
			return true;
		}

		if (this._isCtrlKey(e) && !e.shiftKey && e.altKey && e.keyCode === this.keyCodes.S && app.UI.language.fromURL === 'de' && this._map.getDocType() === 'text') {
			this._map.fire('focussearch');
			e.preventDefault();
			return true;
		}

		if (e.altKey || e.shiftKey) {

			// need to handle Ctrl + Alt + C separately for Firefox
			if (this.keyCodes.C.includes(e.keyCode) && e.altKey) {
				this._map.insertComment();
				return true;
			}

			// Ctrl + Alt
			if (!e.shiftKey) {
				switch (e.keyCode) {
				case this.keyCodes.NUM5[DEFAULT]: // 5
					app.socket.sendMessage('uno .uno:Strikeout');
					return true;
				case this.keyCodes.F: // f but according to value it is F so I guess it is not MAC so safe to Give F
					app.socket.sendMessage('uno .uno:InsertFootnote');
					return true;
				case this.keyCodes.C[DEFAULT]: // c
				case this.keyCodes.M: // m
					this._map.insertComment();
					return true;
				case this.keyCodes.D: // d
					app.socket.sendMessage('uno .uno:InsertEndnote');
					return true;
				case this.keyCodes.P:
					var userListSummary = document.getElementById('userListSummary');
					var userListPopover = document.getElementById('userListPopover');
					// checking case ''(empty string) is because when element loads first time it does not have any inline display style
					var isUserListPopoverVisible = userListPopover.style.display === 'none' || userListPopover.style.display === '';
					// we should only show user list when there are multiple users present and user list is hidden
					var showUserList = isUserListPopoverVisible && userListSummary.hasChildNodes();
					if (showUserList) {
						userListSummary.click();
					}
					else {
						userListPopover.style.display = 'none';
					}
					e.preventDefault();
					return true;
				case this.keyCodes.pageUp:
				case this.keyCodes.pageDown :
					if (this._map.getDocType() === 'spreadsheet') {
						var currentSelectedPart = this._map._docLayer._selectedPart;
						var parts = this._map._docLayer._parts;
						var partToSelect = 0;
						this._map._docLayer._clearReferences();

						if (e.keyCode === this.keyCodes.pageDown) {
							partToSelect = currentSelectedPart != parts - 1 ? currentSelectedPart + 1 : 0;
						}
						else {
							partToSelect = currentSelectedPart != 0 ? currentSelectedPart - 1 : parts - 1;
						}
						this._map.setPart(parseInt(partToSelect), /*external:*/ false, /*calledFromSetPartHandler:*/ true);
						return;
					}

				}
			} else if (e.altKey) {
				switch (e.keyCode) {
					case this.keyCodes.D: // Ctrl + Shift + Alt + d for tile debugging mode
						this._map._debug.toggle();
				}
			}

			return false;
		}
		/* Without specifying the key type, the messages are sent twice (both keydown/up) */
		if (e.type === 'keydown' && window.ThisIsAMobileApp) {
			if (this.keyCodes.C.includes(e.keyCode)) {
				app.socket.sendMessage('uno .uno:Copy');
				return true;
			}
			else if (this.keyCodes.V.includes(e.keyCode)) {
				app.socket.sendMessage('uno .uno:Paste');
				return true;
			}
			else if (this.keyCodes.X.includes(e.keyCode)) {
				app.socket.sendMessage('uno .uno:Cut');
				return true;
			}
			if (window.ThisIsTheAndroidApp)
				e.preventDefault();
		}

		switch (e.keyCode) {
		case this.keyCodes.NUM3[DEFAULT]: // 3
			if (this._map.getDocType() === 'spreadsheet') {
				app.socket.sendMessage('uno .uno:SetOptimalColumnWidthDirect');
				app.socket.sendMessage('commandvalues command=.uno:ViewRowColumnHeaders');
				return true;
			}
			return false;
		case this.keyCodes.NUM5[DEFAULT]: // 5
			if (this._map.getDocType() === 'spreadsheet') {
				app.socket.sendMessage('uno .uno:Strikeout');
				return true;
			}
			return false;
		case this.keyCodes.C[DEFAULT]: // 'C'
		case this.keyCodes.X[DEFAULT]: // 'X'
		case this.keyCodes.C[MAC]: // 'c' Since keydown+c as different value in mac so i had to update the mapping in keyCodes
		case this.keyCodes.X[MAC]: // 'x' same reason as above
		case this.keyCodes.LEFTWINDOWKEY[MAC]: // Left Cmd (Safari)
		case this.keyCodes.RIGHTWINDOWKEY[MAC]: // Right Cmd (Safari)
			// we prepare for a copy or cut event
			this._map.focus();
			// Not sure if the commented code is still used, so I didn't remove it.
			// Anyhow, by when editable area is populated with the focused paragraph
			// we can't select its content or on next editing the content is overwritten.
			// this._map._textInput.select();
			return true;
		case this.keyCodes.P: // p
			this._map.print();
			return true;
		case this.keyCodes.S: // s
			// Save only when not read-only and when HideSaveOption is false.
			if (!this._map.isReadOnlyMode() && !this._map['wopi'].HideSaveOption) {
				this._map.fire('postMessage', {msgId: 'UI_Save', args: { source: 'keyboard' }});
				if (!this._map._disableDefaultAction['UI_Save']) {
					this._map.save(false /* An explicit save should terminate cell edit */,
					               false /* An explicit save should save it again */);
				}
			}
			return true;
		case this.keyCodes.V[DEFAULT]: // v
		case this.keyCodes.V[MAC]: // v (Safari) needs a separate mapping in keyCodes
			return true;
		case this.keyCodes.F1: // f1
			app.socket.sendMessage('uno .uno:NoteVisible');
			return true;
		case this.keyCodes.COMMA: // ,
			app.socket.sendMessage('uno .uno:SubScript');
			return true;
		case this.keyCodes.PERIOD: // .
			app.socket.sendMessage('uno .uno:SuperScript');
			return true;
		}
		if (e.type === 'keypress' && (e.ctrlKey || e.metaKey) &&
			(this.keyCodes.C.includes(e.keyCode) ||this.keyCodes.V.includes(e.keyCode) || this.keyCodes.X.includes(e.keyCode))) {
			// need to handle this separately for Firefox
			return true;
		}
		return false;
	},
});