Generally onKeyPress, onKeyUp, or onKeyDown will only be attached to some sort of form element.
<input type="text" onKeyPress={(event) => console.log(event.key)} />
if you specificly need the keypress from the window you will have to go about it a different way. I’ve included a couple examples on CodeSandbox.