Skip to main content

useIsKeyPressed hook

useIsKeyPressed hook can be use to track if a key's been pressed down

It accepts a key and returns boolean value. If a key being pressed and hold, it will return true.

Examples

Default

import { useIsKeyPress } from 'react-native-molecules/shortcuts-manager';
import { isMac } from 'react-native-molecules/utils';
import { Text } from 'react-native-molecules/components/Text';
import { View } from 'react-native';

const MetaKey = isMac() ? 'Meta' : 'Control';

export const Example = () => {
const isShiftKeyPressed = useIsKeyPress('Shift');
const isControlOrMetaKeyPress = useIsKeyPress(MetaKey);
const isAltKeyPressed = useIsKeyPress('Alt');

return (
<View>
<Text>
isShiftKeyPressed: <Text>{String(isShiftKeyPressed)}</Text>
</Text>
<Text>
isControlOrMetaKeyPress: <Text>{String(isControlOrMetaKeyPress)}</Text>
</Text>
<Text>
isAltKeyPressed: <Text>{String(isAltKeyPressed)}</Text>
</Text>
</View>
);
};