forked from jaywcjlove/hotkeys-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.d.ts
180 lines (169 loc) · 5.08 KB
/
index.d.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
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
export interface HotkeysEvent {
key: string;
keys: number[];
method: KeyHandler;
mods: number[];
scope: string;
shortcut: string;
}
export interface KeyHandler {
(keyboardEvent: KeyboardEvent, hotkeysEvent: HotkeysEvent): void | boolean;
}
type Options = {
scope?: string;
element?: HTMLElement | null;
keyup?: boolean | null;
keydown?: boolean | null;
capture?: boolean
splitKey?: string;
}
export interface Hotkeys {
(key: string, method: KeyHandler): void;
(key: string, scope: string, method: KeyHandler): void;
(key: string, options: Options, method: KeyHandler): void;
shift: boolean;
ctrl: boolean;
alt: boolean;
option: boolean;
control: boolean;
cmd: boolean;
command: boolean;
keyMap: Record<string, number>;
modifier: Record<string, number>;
modifierMap: Record<string, number | string>;
/**
* Use the `hotkeys.setScope` method to set scope. There can only be one active scope besides 'all'. By default 'all' is always active.
*
* ```js
* // Define shortcuts with a scope
* hotkeys('ctrl+o, ctrl+alt+enter', 'issues', function() {
* console.log('do something');
* });
* hotkeys('o, enter', 'files', function() {
* console.log('do something else');
* });
*
* // Set the scope (only 'all' and 'issues' shortcuts will be honored)
* hotkeys.setScope('issues'); // default scope is 'all'
* ```
*/
setScope(scopeName: string): void;
/**
* Use the `hotkeys.getScope` method to get scope.
*
* ```js
* hotkeys.getScope();
* ```
*/
getScope(): string;
/**
* Use the `hotkeys.deleteScope` method to delete a scope. This will also remove all associated hotkeys with it.
*
* ```js
* hotkeys.deleteScope('issues');
* ```
* You can use second argument, if need set new scope after deleting.
*
* ```js
* hotkeys.deleteScope('issues', 'newScopeName');
* ```
*/
deleteScope(scopeName: string, newScopeName?: string): void;
/**
* Relinquish HotKeys’s control of the `hotkeys` variable.
*
* ```js
* var k = hotkeys.noConflict();
* k('a', function() {
* console.log("do something")
* });
*
* hotkeys()
* // -->Uncaught TypeError: hotkeys is not a function(anonymous function)
* // @ VM2170:2InjectedScript._evaluateOn
* // @ VM2165:883InjectedScript._evaluateAndWrap
* // @ VM2165:816InjectedScript.evaluate @ VM2165:682
* ```
*/
noConflict(): Hotkeys;
/**
* trigger shortcut key event
*
* ```js
* hotkeys.trigger('ctrl+o');
* hotkeys.trigger('ctrl+o', 'scope2');
* ```
*/
trigger(shortcut: string, scope?: string): void;
unbind(key?: string): void;
unbind(key: string, scopeName: string): void;
unbind(key: string, scopeName: string, method: KeyHandler): void;
unbind(key: string, method: KeyHandler): void;
/** For example, `hotkeys.isPressed(77)` is true if the `M` key is currently pressed. */
isPressed(keyCode: number): boolean;
/** For example, `hotkeys.isPressed('m')` is true if the `M` key is currently pressed. */
isPressed(keyCode: string): boolean;
/**
* Returns an array of key codes currently pressed.
*
* ```js
* hotkeys('command+ctrl+shift+a,f', function() {
* console.log(hotkeys.getPressedKeyCodes()); //=> [17, 65] or [70]
* })
* ```
*/
getPressedKeyCodes(): number[];
/**
* Returns an array of key codes currently pressed.
*
* ```js
* hotkeys('command+ctrl+shift+a,f', function() {
* console.log(hotkeys.getPressedKeyString()); //=> ['⌘', '⌃', '⇧', 'A', 'F']
* })
* ```
*/
getPressedKeyString(): string[];
/**
* Get a list of all registration codes.
*
* ```js
* hotkeys('command+ctrl+shift+a,f', function() {
* console.log(hotkeys.getAllKeyCodes());
* // [
* // { scope: 'all', shortcut: 'command+ctrl+shift+a', mods: [91, 17, 16], keys: [91, 17, 16, 65] },
* // { scope: 'all', shortcut: 'f', mods: [], keys: [42] }
* // ]
* })
* ```
*
*/
getAllKeyCodes(): Omit<HotkeysEvent, 'method' | 'key'>;
/**
* By default hotkeys are not enabled for `INPUT` `SELECT` `TEXTAREA` elements.
* `Hotkeys.filter` to return to the `true` shortcut keys set to play a role,
* `false` shortcut keys set up failure.
*
* ```js
* hotkeys.filter = function(event){
* return true;
* }
* //How to add the filter to edit labels. <div contentEditable="true"></div>
* //"contentEditable" Older browsers that do not support drops
* hotkeys.filter = function(event) {
* var target = event.target || event.srcElement;
* var tagName = target.tagName;
* return !(target.isContentEditable || tagName == 'INPUT' || tagName == 'SELECT' || tagName == 'TEXTAREA');
* }
*
* hotkeys.filter = function(event){
* var tagName = (event.target || event.srcElement).tagName;
* hotkeys.setScope(/^(INPUT|TEXTAREA|SELECT)$/.test(tagName) ? 'input' : 'other');
* return true;
* }
* ```
*/
filter(event: KeyboardEvent): boolean;
}
// https://github.com/eiriklv/react-masonry-component/issues/57
declare var hotkeys: Hotkeys;
export default hotkeys;