UserInputManager
UserInputManager
allows apps to override the default behavior when D-pad events are received.
Usage
The following code example show how to use addListener
to register events:
import {
EventSubscription,
useAddUserInputListenerCallback,
UserInputEvent,
UserInputEventName,
UserInputManager
} from '@amzn/react-native-kepler';
import * as React from 'react';
import { useEffect, useState } from 'react';
import { Text, View } from 'react-native';
const supportedEvents = [
UserInputEventName.Up,
UserInputEventName.Down,
UserInputEventName.Left,
UserInputEventName.Right,
UserInputEventName.Select,
UserInputEventName.Back,
UserInputEventName.Menu,
UserInputEventName.PlayPause,
UserInputEventName.SkipBackward,
UserInputEventName.SkipForward,
UserInputEventName.PageUp,
UserInputEventName.PageDown,
UserInputEventName.PageLeft,
UserInputEventName.PageRight,
UserInputEventName.Info,
UserInputEventName.More
];
type KeyUsageMap = {
[key in UserInputEventName]?: { pressed: boolean, count: number }
};
const App = () => {
const addUserInputListenerCallback = useAddUserInputListenerCallback();
const eventSubscriptions: EventSubscription[] = [];
const [state, setKeyState] = useState<KeyUsageMap>(
supportedEvents.reduce(
(obj, event: UserInputEventName) => ({
...obj,
[event]: { pressed: false, count: 0 }
}),
{}
)
);
useEffect(() => {
supportedEvents.forEach((name, index) => {
eventSubscriptions.push(addUserInputListenerCallback(name, (event: UserInputEvent) => {
console.log(name, event);
update(name, event);
return true;
}));
});
return () => {
eventSubscriptions.forEach((subscription) => subscription.remove());
};
}, []);
const update = (name: UserInputEventName, event: UserInputEvent) => {
setKeyState((oldState: KeyUsageMap) => {
const newState = { ...oldState };
const oldCount = oldState[name]?.count ?? 0;
if (event.phase === 'PRESSED') {
newState[name] = { count: oldCount + 1, pressed: true };
} else if (event.phase === 'RELEASED') {
newState[name] = { count: oldCount, pressed: false };
}
return newState;
});
};
return (
<View style={DEFAULT_STYLES.CONTAINER}>
<View style={styles.innerContainer}>
<Text style={styles.noteText}>
Press a key on the remote. To navigate back, press the
"Back" button twice.
</Text>
<View style={styles.itemContainer}>
{Object.entries(state).map(([name, info]: any) => (
<View
style={[
styles.item,
info.pressed ? styles.itemPressed : {}
]}>
<Text style={styles.itemKeyText}>{name}</Text>
<Text style={styles.itemSubText}>
Press Count: {info.count}
</Text>
</View>
))}
</View>
</View>
</View>
);
};
export default App;
Reference
Methods
UserInputManager.addListener(eventName, callback)
Deprecated: Use the useAddUserInputListenerCallback()
hook instead.
This method allows app to registers listener for specific event, following is a list of all supported events in Kepler:
UserInputEventName.Up,
UserInputEventName.Down,
UserInputEventName.Left,
UserInputEventName.Right,
UserInputEventName.Select,
UserInputEventName.Back,
UserInputEventName.Menu,
UserInputEventName.PlayPause,
UserInputEventName.SkipBackward,
UserInputEventName.SkipForward,
UserInputEventName.PageUp,
UserInputEventName.PageDown,
UserInputEventName.PageLeft,
UserInputEventName.PageRight,
UserInputEventName.Info,
UserInputEventName.More
useAddUserInputListenerCallback
This custom hook returns a callback that, when invoked, registers a listener for specific event. When calling this hook, make sure to follows the established rules for hooks. For more information, seeRules of Hooks.
The returned callback function takes two paramenters: the UserInputEventName
to listen to, and a callback function to invoke when the desired UserInputEventName
is pressed.
The following is a list of all supported events in Kepler:
UserInputEventName.Up,
UserInputEventName.Down,
UserInputEventName.Left,
UserInputEventName.Right,
UserInputEventName.Select,
UserInputEventName.Back,
UserInputEventName.Menu,
UserInputEventName.PlayPause,
UserInputEventName.SkipBackward,
UserInputEventName.SkipForward,
UserInputEventName.PageUp,
UserInputEventName.PageDown,
UserInputEventName.PageLeft,
UserInputEventName.PageRight,
UserInputEventName.Info,
UserInputEventName.More
Last updated: Sep 30, 2025