Use UIKeyCommands
(iOS).
$ npm install react-native-keycommands --save
$ react-native link react-native-keycommands
- In XCode, in the project navigator, right click
Libraries
➜Add Files to [your project's name]
- Go to
node_modules
➜react-native-keycommands
and addKeyCommands.xcodeproj
- In XCode, in the project navigator, select your project. Add
libKeyCommands.a
to your project'sBuild Phases
➜Link Binary With Libraries
- Run your project (
Cmd+R
)<
Not need setup.
import React from 'react';
import KeyCommands, { constants } from 'react-native-keycommands';
const keyCommands = [
{
input: 'a',
modifierFlags: 0, // without flags
discoverabilityTitle: 'a pressed'
},
{
input: 'a',
modifierFlags: constants.keyModifierCommand,
discoverabilityTitle: 'CMD + a pressed'
},
{
input: 'a',
modifierFlags: constants.keyModifierCommand | constants.keyModifierAlternate,
discoverabilityTitle: 'CMD + ALTERNATE + a pressed'
}
];
export default class App extends React.Component {
onKeyCommand = ({ nativeEvent }) => {
console.log('event: ', nativeEvent);
};
render() {
<KeyCommands
style={{ flex: 1 }}
keyCommands={keyCommands}
onKeyCommand={this.onKeyCommand}
/>
}
};