admin管理员组文章数量:1317572
I’m trying to find a way to get the keyboard to focus on the YouTube player in my page. This could, for example, make it easy to use the space bar to play/pause the video.
Here’s an example: Testing Embed Keyboard Focus (CodePen)
As it is, I’m having to click on the player for it to be able to accept keyboard shortcuts. Because this is not ideal, I’m hoping for a workaround using Javascript or jQuery to set the focus on the video.
I’m aware that – as an alternative – I could use the spacebar key to call the player object's playVideo
or pauseVideo
methods, but that still wouldn’t get me access to the full list of the YouTube player’s keyboard controls.
And if this is simply impossible, I’ll understand. But it will be nice to know. Thanks!
I’m trying to find a way to get the keyboard to focus on the YouTube player in my page. This could, for example, make it easy to use the space bar to play/pause the video.
Here’s an example: Testing Embed Keyboard Focus (CodePen)
As it is, I’m having to click on the player for it to be able to accept keyboard shortcuts. Because this is not ideal, I’m hoping for a workaround using Javascript or jQuery to set the focus on the video.
I’m aware that – as an alternative – I could use the spacebar key to call the player object's playVideo
or pauseVideo
methods, but that still wouldn’t get me access to the full list of the YouTube player’s keyboard controls.
And if this is simply impossible, I’ll understand. But it will be nice to know. Thanks!
Share Improve this question asked Nov 24, 2015 at 16:52 Joel FarrisJoel Farris 5101 gold badge7 silver badges23 bronze badges 1- What I’d ultimately like is a way to get the controls to show. Either permanently or when the user engages with a separate in-page element. The play/pause and volume up/down keyboard shortcuts show the controls, so that’s why I wanted to use them. (The API functions for each of these actually don’t show the controls.) Perhaps I should start another question for that: Is there any way to make the player controls show in a YouTube embed. – Joel Farris Commented Dec 1, 2015 at 21:15
2 Answers
Reset to default 4 +200You can do something like this to implement whatever hotkeys you want to enable for the player at any rate.
http://codepen.io/anon/pen/jbgeYo
Basically you create an input (can style it so that it blends in, you can't do display:none as that won't let you assign focus), and then attach a keypress event to that input. You then can call functions on the "player" API object which allows you to play/pause etc.
var dummy=document.getElementById("dummyFocus");
dummy.focus();
dummy.addEventListener("keypress",function(event){
if(event.keyCode== 32){
if(player.getPlayerState() == 1){
player.pauseVideo();
}
else{
player.playVideo();
}
}
});
Insert that after you create your player object, and add an input (I used type="button") to the page with id="dummyFocus"
Edit as needed, but that's a workaround anyway.
In order to achieve this you would have to focus()
document embedded in the Youtube's iframe
. This is restricted by Same-origin policy for security reasons.
Here's a piece of code for pleteness
function onPlayerReady(event) {
document.querySelector('#player').contentDocument.body.focus();
}
And it would throw
Uncaught SecurityError: Failed to read the 'contentDocument' property from 'HTMLIFrameElement':
本文标签: javascriptSetting Keyboard Focus to YouTube EmbedStack Overflow
版权声明:本文标题:javascript - Setting Keyboard Focus to YouTube Embed - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742021671a2414792.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论