-
Notifications
You must be signed in to change notification settings - Fork 97
/
index.js
56 lines (49 loc) · 1.56 KB
/
index.js
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
import { DeviceEventEmitter, NativeEventEmitter, NativeModules, Platform } from 'react-native';
class KeyEvent {
onKeyDownListener(cb) {
this.removeKeyDownListener();
if (Platform.OS === "ios") {
let keyEvent = new NativeEventEmitter(NativeModules.RNKeyEvent);
this.listenerKeyDown = keyEvent.addListener('onKeyDown', cb);
} else {
this.listenerKeyDown = DeviceEventEmitter.addListener('onKeyDown', cb);
}
}
removeKeyDownListener() {
if (this.listenerKeyDown) {
this.listenerKeyDown.remove();
this.listenerKeyDown = null;
}
}
onKeyUpListener(cb) {
this.removeKeyUpListener();
if (Platform.OS === "ios") {
let keyEvent = new NativeEventEmitter(NativeModules.RNKeyEvent);
this.listenerKeyUp = keyEvent.addListener('onKeyUp', cb);
} else {
this.listenerKeyUp = DeviceEventEmitter.addListener('onKeyUp', cb);
}
}
removeKeyUpListener() {
if (this.listenerKeyUp) {
this.listenerKeyUp.remove();
this.listenerKeyUp = null;
}
}
onKeyMultipleListener(cb) {
this.removeKeyMultipleListener();
if (Platform.OS === "ios") {
let keyEvent = new NativeEventEmitter(NativeModules.RNKeyEvent);
this.listenerKeyMultiple = keyEvent.addListener('onKeyMultiple', cb);
} else {
this.listenerKeyMultiple = DeviceEventEmitter.addListener('onKeyMultiple', cb);
}
}
removeKeyMultipleListener() {
if (this.listenerKeyMultiple) {
this.listenerKeyMultiple.remove();
this.listenerKeyMultiple = null;
}
}
}
export default new KeyEvent();