React not responding to key down event

后端 未结 4 944
死守一世寂寞
死守一世寂寞 2021-02-02 17:16

I\'m trying to implement some very basic key press detection and I can\'t get it to work at all. I have a bare component that should be picking up on the onKeyDown

4条回答
  •  孤城傲影
    2021-02-02 17:45

    In an app my team is working on, we're using react-hotkey. Unfortunately, React doesn't seem to support mixins with ES6 syntax, but if you're cool with some babel, you could give it a try.

    let App = React.createClass({
      mixins: [hotkey.Mixin('handleKeyDown')],
    
      componentDidMount() {
        hotkey.activate();
      },
    
      componentWillUnmount() {
        hotkey.disable();
      },
    
      handleKeyDown(event) {
        console.log('handling a key press');
      },
    
      render() {
        return (
          
        );
      }
    });
    
    React.render(, document.getElementById('app'));
    

提交回复
热议问题