I am trying to detect a key press anywhere on the page in an app built with React.
I remember it being quite easy in jQuery, but I wanted to see if it could be done in React. I didn't want to make a 'frankenstein' project i.e. have some parts in react and some parts in jQuery, if at all avoidable.
So far I have:
export default function App() {
const handleKeyPress = (event) => {
console.log(event.keyCode);
}
return (
<div
onKeyDown={handleKeyPress}
tabIndex="0"
>
<Header page_num={100}/>
</div>
);
}
But it only seems to work when a user clicks on the actual div in the page, then presses a key.
I want the key press to be detected without the user having to click anywhere on the page.