-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
keyobserver.ts
87 lines (76 loc) · 2.43 KB
/
keyobserver.ts
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
/**
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module engine/view/observer/keyobserver
*/
import DomEventObserver from './domeventobserver.js';
import type DomEventData from './domeventdata.js';
import { getCode, type KeystrokeInfo } from '@ckeditor/ckeditor5-utils';
/**
* Observer for events connected with pressing keyboard keys.
*
* Note that this observer is attached by the {@link module:engine/view/view~View} and is available by default.
*/
export default class KeyObserver extends DomEventObserver<'keydown' | 'keyup', KeystrokeInfo & { keystroke: number }> {
/**
* @inheritDoc
*/
public readonly domEventType = [ 'keydown', 'keyup' ] as const;
/**
* @inheritDoc
*/
public onDomEvent( domEvt: KeyboardEvent ): void {
const data = {
keyCode: domEvt.keyCode,
altKey: domEvt.altKey,
ctrlKey: domEvt.ctrlKey,
shiftKey: domEvt.shiftKey,
metaKey: domEvt.metaKey,
get keystroke() {
return getCode( this );
}
};
this.fire( domEvt.type, domEvt, data );
}
}
/**
* Fired when a key has been pressed.
*
* Introduced by {@link module:engine/view/observer/keyobserver~KeyObserver}.
*
* Note that because {@link module:engine/view/observer/keyobserver~KeyObserver} is attached by the
* {@link module:engine/view/view~View} this event is available by default.
*
* @see module:engine/view/observer/keyobserver~KeyObserver
* @eventName module:engine/view/document~Document#keydown
*/
export type ViewDocumentKeyDownEvent = {
name: 'keydown';
args: [ data: KeyEventData ];
};
/**
* Fired when a key has been released.
*
* Introduced by {@link module:engine/view/observer/keyobserver~KeyObserver}.
*
* Note that because {@link module:engine/view/observer/keyobserver~KeyObserver} is attached by the
* {@link module:engine/view/view~View} this event is available by default.
*
* @see module:engine/view/observer/keyobserver~KeyObserver
* @eventName module:engine/view/document~Document#keyup
*/
export type ViewDocumentKeyUpEvent = {
name: 'keyup';
args: [ data: KeyEventData ];
};
/**
* The value of both events - {@link ~ViewDocumentKeyDownEvent} and {@link ~ViewDocumentKeyUpEvent}.
*/
export interface KeyEventData extends DomEventData<KeyboardEvent>, KeystrokeInfo {
/**
* Code of the whole keystroke. See {@link module:utils/keyboard~getCode}.
*/
keystroke: number;
}