Skip to content

Commit e8f3da5

Browse files
committed
chore: handle long press in RemonteControlManager
1 parent e3e02b0 commit e8f3da5

5 files changed

Lines changed: 87 additions & 0 deletions

File tree

packages/example/src/components/configureRemoteControl.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ SpatialNavigation.configureRemoteControl({
1010
[SupportedKeys.Up]: Directions.UP,
1111
[SupportedKeys.Down]: Directions.DOWN,
1212
[SupportedKeys.Enter]: Directions.ENTER,
13+
[SupportedKeys.LongEnter]: Directions.LONG_ENTER,
1314
[SupportedKeys.Back]: null,
1415
};
1516

packages/example/src/components/remote-control/RemoteControlManager.android.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,26 @@ import KeyEvent from 'react-native-keyevent';
33
import { RemoteControlManagerInterface } from './RemoteControlManager.interface';
44
import CustomEventEmitter from './CustomEventEmitter';
55

6+
const LONG_PRESS_DURATION = 500;
7+
68
class RemoteControlManager implements RemoteControlManagerInterface {
79
constructor() {
810
KeyEvent.onKeyDownListener(this.handleKeyDown);
11+
KeyEvent.onKeyUpListener(this.handleKeyUp);
912
}
1013

1114
private eventEmitter = new CustomEventEmitter<{ keyDown: SupportedKeys }>();
1215

16+
private isEnterKeyDownPressed = false;
17+
private longEnterTimeout: NodeJS.Timeout | null = null;
18+
19+
private handleLongEnter = () => {
20+
this.longEnterTimeout = setTimeout(() => {
21+
this.eventEmitter.emit('keyDown', SupportedKeys.LongEnter);
22+
this.longEnterTimeout = null;
23+
}, LONG_PRESS_DURATION);
24+
};
25+
1326
private handleKeyDown = (keyEvent: { keyCode: number }) => {
1427
const mappedKey = {
1528
21: SupportedKeys.Left,
@@ -26,9 +39,36 @@ class RemoteControlManager implements RemoteControlManagerInterface {
2639
return;
2740
}
2841

42+
if (mappedKey === SupportedKeys.Enter) {
43+
if (!this.isEnterKeyDownPressed) {
44+
this.isEnterKeyDownPressed = true;
45+
this.handleLongEnter();
46+
}
47+
return;
48+
}
49+
2950
this.eventEmitter.emit('keyDown', mappedKey);
3051
};
3152

53+
private handleKeyUp = (keyEvent: { keyCode: number }) => {
54+
const mappedKey = {
55+
66: SupportedKeys.Enter,
56+
23: SupportedKeys.Enter,
57+
}[keyEvent.keyCode];
58+
59+
if (!mappedKey) {
60+
return;
61+
}
62+
63+
if (mappedKey === SupportedKeys.Enter) {
64+
this.isEnterKeyDownPressed = false;
65+
if (this.longEnterTimeout) {
66+
clearTimeout(this.longEnterTimeout);
67+
this.eventEmitter.emit('keyDown', mappedKey);
68+
}
69+
}
70+
};
71+
3272
addKeydownListener = (listener: (event: SupportedKeys) => boolean) => {
3373
this.eventEmitter.on('keyDown', listener);
3474
return listener;

packages/example/src/components/remote-control/RemoteControlManager.ios.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,18 @@ class RemoteControlManager implements RemoteControlManagerInterface {
1919
up: SupportedKeys.Up,
2020
down: SupportedKeys.Down,
2121
select: SupportedKeys.Enter,
22+
longSelect: SupportedKeys.LongEnter,
2223
}[evt.eventType];
2324

2425
if (!mappedKey) {
2526
return;
2627
}
2728

29+
// We only want to handle keydown fon long select
30+
if (mappedKey === SupportedKeys.LongEnter && evt.eventKeyAction === 1) {
31+
return;
32+
}
33+
2834
this.eventEmitter.emit('keyDown', mappedKey);
2935
};
3036

packages/example/src/components/remote-control/RemoteControlManager.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,26 @@ import { SupportedKeys } from './SupportedKeys';
22
import { RemoteControlManagerInterface } from './RemoteControlManager.interface';
33
import CustomEventEmitter from './CustomEventEmitter';
44

5+
const LONG_PRESS_DURATION = 500;
6+
57
class RemoteControlManager implements RemoteControlManagerInterface {
68
constructor() {
79
window.addEventListener('keydown', this.handleKeyDown);
10+
window.addEventListener('keyup', this.handleKeyUp);
811
}
912

1013
private eventEmitter = new CustomEventEmitter<{ keyDown: SupportedKeys }>();
1114

15+
private isEnterKeyDown = false;
16+
private longEnterTimeout: NodeJS.Timeout | null = null;
17+
18+
private handleLongEnter = () => {
19+
this.longEnterTimeout = setTimeout(() => {
20+
this.eventEmitter.emit('keyDown', SupportedKeys.LongEnter);
21+
this.longEnterTimeout = null;
22+
}, LONG_PRESS_DURATION);
23+
};
24+
1225
private handleKeyDown = (event: KeyboardEvent) => {
1326
const mappedKey = {
1427
ArrowRight: SupportedKeys.Right,
@@ -23,9 +36,35 @@ class RemoteControlManager implements RemoteControlManagerInterface {
2336
return;
2437
}
2538

39+
if (mappedKey === SupportedKeys.Enter) {
40+
if (!this.isEnterKeyDown) {
41+
this.isEnterKeyDown = true;
42+
this.handleLongEnter();
43+
}
44+
return;
45+
}
46+
2647
this.eventEmitter.emit('keyDown', mappedKey);
2748
};
2849

50+
private handleKeyUp = (event: KeyboardEvent) => {
51+
const mappedKey = {
52+
Enter: SupportedKeys.Enter,
53+
}[event.code];
54+
55+
if (!mappedKey) {
56+
return;
57+
}
58+
59+
if (mappedKey === SupportedKeys.Enter) {
60+
this.isEnterKeyDown = false;
61+
if (this.longEnterTimeout) {
62+
clearTimeout(this.longEnterTimeout);
63+
this.eventEmitter.emit('keyDown', mappedKey);
64+
}
65+
}
66+
};
67+
2968
addKeydownListener = (listener: (event: SupportedKeys) => boolean) => {
3069
this.eventEmitter.on('keyDown', listener);
3170
return listener;

packages/example/src/components/remote-control/SupportedKeys.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ export enum SupportedKeys {
44
Left = 'Left',
55
Right = 'Right',
66
Enter = 'Enter',
7+
LongEnter = 'LongEnter',
78
Back = 'Back',
89
}

0 commit comments

Comments
 (0)