microsoft / react-native-macos

A framework for building native macOS apps with React.
https://microsoft.github.io/react-native-windows/
MIT License
3.51k stars 135 forks source link

"Global" keyboard listening #2220

Open George3d6 opened 4 days ago

George3d6 commented 4 days ago

Reading through https://github.com/microsoft/react-native-macos/issues/966 and https://github.com/microsoft/react-native-macos/issues/823 It seemed that the recommended way to get global keyboard listeners was using onKeyDown on a top-level View

This seems not to work however I found:

onKeyPress={(e)=> {
        console.log(e.nativeEvent.key)
    }}

Works just fine (even though the type of View doesn't specify it ... however it only works after I e.g. click a specific TextInput (even if I move it out of focus later).

Is there an updated workaround to getting global keyboard listening to work (thus allowing things like hotkeys/shortcuts) ?

Saadnajmi commented 4 days ago

onKeyPress is a TextInput only prop / event which I suppose bubbled up without us realizing.. I wouldn't rely on that. We inherit that prop from React Native on mobile, and it doesn't really fit into the desktop keyboard API.

For global keyboard listening, since React Native is mostly only Root view and down, I think you still have to rely on implementing that natively with something like https://github.com/sindresorhus/KeyboardShortcuts unfortunately.