2

I want to listen for a keydown event of an iframe in reactjs. In my iframe i have video embedded. Now when the video is playing i want to handle keyboard events.Can any one please help me how to listen for keyboard events. This is my code

class VideoDetail extends Component {

videoLoad(e) {
    console.log('videoLoaded');
    this.videoFrame.addEventListener('keydown',this.onVideoDown);
}

onVideoDown(e) {
    console.log('key pressed'); // This is not invoking
}

componentDidMount() {
    console.log(this.videoFrame);
}
render() {
    const video = this.props.video;
    if (!video) {
        return <div>Loading...</div>;
    }

    const videoId = video.id.videoId;
    const url = `https://www.youtube.com/embed/${videoId}`;
    return (
        <div className="video-detail col-md-8">
            <div className="embed-responsive embed-responsive-16by9">
                <iframe ref={(iframe) => { this.videoFrame = iframe; console.log(iframe); }} className="embed-responsive-item" src={url} onLoad={(e) => this.videoLoad(e)}></iframe>
            </div>
            <div className="details">
                <div>{video.snippet.title}</div>
                <div>{video.snippet.description}</div>
            </div>
        </div>
    );
}

}
Aruna
  • 11,441
  • 3
  • 24
  • 39
Trinu
  • 1,133
  • 3
  • 12
  • 34

1 Answers1

1

You can catch the iframe keydown event with iframe.contentWindow.document as below using either e.target or this.videoFrame with the ref.

Modified code

videoLoad(e) {
    console.log('videoLoaded');
    var iframe = e.target; // it is equal to "this.videoFrame" and so, you can avoid using "ref"
    //this.videoFrame.addEventListener('keydown',this.onVideoDown);
    iframe.contentWindow.document.addEventListener('keydown', this.onVideoDown);
}
Aruna
  • 11,441
  • 3
  • 24
  • 39
  • Thanks for your reply aruna. I am getting this error after adding your line.Uncaught DOMException: Blocked a frame with origin "http://localhost:8080" from accessing a cross-origin frame. – Trinu Apr 23 '17 at 11:47
  • @Trinu, This is coming under `Same-origin security policy` as you are trying to access one domain from another domain using javascript. If both domains are same, then the above code will work without any issues otherwise you have to disable the browser security if you are just creating the page for testing only. Please have a look at this url, http://stackoverflow.com/a/25098153/7055233 – Aruna Apr 23 '17 at 12:03
  • i have executed this command in osx -a Google\ Chrome --args --disable-web-security --user-data-dir. This time i am not getting error but keydown event is not firing.. unable to listen the keydown event. – Trinu Apr 23 '17 at 12:23
  • I hope you tried this, `$ open -a Google\ Chrome --args --disable-web-security --user-data-dir` in OSX – Aruna Apr 23 '17 at 12:27
  • Can you give me a sample data used for `this.props.video` ? – Aruna Apr 23 '17 at 12:33
  • Its just an object contains id... const url = `https://www.youtube.com/embed/${videoId}`; that url i am using in iframe src={url} you can try with any youtube id url = https://www.youtube.com/embed/-VMgTCnFGys – Trinu Apr 23 '17 at 12:37
  • @Trinu, I checked this in my windows machine and works as expected without any issues. I can see `key pressed` printed in the console. – Aruna Apr 23 '17 at 12:49
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/142395/discussion-between-trinu-and-aruna). – Trinu Apr 23 '17 at 12:56
  • Thanks Aruna its working.. Thanks a lot for your answer. – Trinu Apr 23 '17 at 13:00