0

I want my app to detect left and right arrow key presses. I got it to work, however, I have to click on the App component for it to register. How can I handle this event without click anywhere / the entire document? How am I able to detect exactly which keys are pressed?

  // App.js

  keyHandler = e => {
    console.log(e);
  };

  render() {
    return (
      <div onKeyDown={this.keyHandler} tabIndex="0">

1 Answer 1

2
window.addEventListener("click", function(event){});

You can do the same with any event, like keypress or whatever.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! I'm assuming there isn't a React event listener for this?
No, react does not provide listener on its own. Its just not what its made for. But if you need to connect that with react you can use state whether it be class component or just functional with useState( )
Please mark as answered if it was what you were looking for. :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.