I couldn't use <video>
tag for youtube video so I embed youtube inside a <iframe>
as below to play a youtube video:
<iframe type="text/html"
width="640"
height="385"
src="http://www.youtube.com/embed/VIDEO_ID"
frameborder="0">
</iframe>
It works fine to play the video. But I need to support press escape
key to stop playing the video. Below code works for a <video>
tag but it doesn't work for the iframe when the video is playing. It seems that the video takes keyboard control while playing. Is there a way for me to listen on keyboard event of the youtube iframe?
window.addEventListener('keydown',(e) => {
if (e.key === 'Escape') {
...
}
});
I tried to play the video via youtube api iframe, but the keydown event is not triggered when the video is playing. This code can be found: https://codepen.io/zhaoyi0113/pen/YJvJay. The event listener is not triggered if the video get focus and playing.