Skip to content

Instantly share code, notes, and snippets.

@aleclarson
Last active September 12, 2021 01:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aleclarson/6c609884fc08c20492c8722eed17acc1 to your computer and use it in GitHub Desktop.
Save aleclarson/6c609884fc08c20492c8722eed17acc1 to your computer and use it in GitHub Desktop.

<HotKey> for react-native-macos

There are two types of hotkeys: global and local.

  • Global hotkeys work even when the application is inactive.
  • Local hotkeys only work when the application is active.

The implementation for global hotkeys would be adapted from davedelong/DDHotKey.

The implementation for local hotkeys would use the RCTKeyCommands class that already exists.

Component API

import {HotKey} from 'react-native-macos'
import {copy, paste} from 'my-app'

// Mount this component to enable the hotkeys inside.
const CopyPaste = () => (
  <>
    <HotKey bind="cmd+c" onPress={copy} />
    <HotKey bind="cmd+p" onPress={paste} />
  </>
)

Hotkeys are local by default. Use the global prop to make them work when the app is inactive.

<HotKey global={true} bind="cmd+shift+l" onPress={toggleLights} />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment