最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to handle onKeyDown event on div in ReactJS - Stack Overflow

programmeradmin3浏览0评论

I'm trying to handle key event when load page ponent. First, I have a router:

<Router>
    <Route exact path="/" ponent={Home} />
</Router>

In home ponent, I try to bind onKeyPress in div element but it's not work. I bind it on input element, it's worked.

return (
        <div onKeyDown={this.__handleKeyDown} className="container" style={{ backgroundImage: `url(${this.state.backgroundbanner})` }}>
            <input
                className="hidden"
                onKeyDown={this.__handleKeyDown}
                ref={(input) => { this.dummyInput = input; }}
                />
            <div className="container-shadow">
                <h1 className="main-title">{this.state.title}</h1>
                <h3 className="main-description">{this.state.description}</h3>
                <ListMovie cursor={ cursor } />
            </div>
        </div>
    )

How to bind event onKeyDown on div element or how to bind key event when load a page ponent in Route. Because, input element can be out-focus and this key event cannot be excute.

Thanks.

I'm trying to handle key event when load page ponent. First, I have a router:

<Router>
    <Route exact path="/" ponent={Home} />
</Router>

In home ponent, I try to bind onKeyPress in div element but it's not work. I bind it on input element, it's worked.

return (
        <div onKeyDown={this.__handleKeyDown} className="container" style={{ backgroundImage: `url(${this.state.backgroundbanner})` }}>
            <input
                className="hidden"
                onKeyDown={this.__handleKeyDown}
                ref={(input) => { this.dummyInput = input; }}
                />
            <div className="container-shadow">
                <h1 className="main-title">{this.state.title}</h1>
                <h3 className="main-description">{this.state.description}</h3>
                <ListMovie cursor={ cursor } />
            </div>
        </div>
    )

How to bind event onKeyDown on div element or how to bind key event when load a page ponent in Route. Because, input element can be out-focus and this key event cannot be excute.

Thanks.

Share Improve this question asked Nov 7, 2017 at 10:22 Pham Minh TanPham Minh Tan 2,0967 gold badges27 silver badges39 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

The reason as to why it doesn't work is that the div element requires the tabIndex attribute in order to be focusable and to handle keyDown events.

<div tabIndex="1" onKeyDown={this.__handleKeyDown}></div>

Approach 1:

For the event to trigger, your div needs to be selected. To do this you need to focus it in the ponentDidMount event. And to do this you need a ref to your div.

Step 1: get a ref to your div

<div onKeyDown={this.__handleKeyDown} ref={(c) => {this.div = c;}}>

Step 2: Focus it on load

ponentDidMount() {
  this.div.focus();
}

Approach 2:

Listen to events on the entire document

ponentDidMount() {
    document.addEventListener('keydown', this.onKeyDown);
}

ponentWillUnmount() {
    document.removeEventListener('keydown', this.onKeyDown);
}
发布评论

评论列表(0)

  1. 暂无评论