简体   繁体   English

在全屏模式下观看 HTML5 视频时如何捕获键盘事件?

[英]How do I capture keyboard events while watching an HTML5 video in fullscreen mode?

I need to know when the user presses the escape key when watching an HTML5 video in fullscreen mode.我需要知道用户在全屏模式下观看 HTML5 视频时何时按下退出键。 Unfortunately any configured listeners on the document don't apply since when the user is watching an HTML5 video in fullscreen mode the system focus is on the native video player rather than the browser.不幸的是,文档上配置的任何监听器都不适用,因为当用户在全屏模式下观看 HTML5 视频时,系统的焦点是本地视频播放器而不是浏览器。

An alternative is listening for when focus reverts back from the native video player to the browser, but I'm unsure as to how I would capture this.另一种方法是监听焦点何时从本机视频播放器返回到浏览器,但我不确定我将如何捕获它。

document.addEventListener('keydown', function (e) { console.log(e.keyCode); }, false);

The above successfully logs to the console when I press keys so long as I'm in the browser.只要我在浏览器中,当我按下按键时,上面的内容就会成功登录到控制台。 As soon as an HTML5 video enters fullscreen mode, the browser obviously can no longer log key codes to the console.一旦 HTML5 视频进入全屏模式,浏览器显然无法再将关键代码记录到控制台。

What I'm trying to do is transition from one UI to another after exiting fullscreen mode.我要做的是在退出全屏模式后从一个 UI 过渡到另一个 UI。

EDIT编辑

Potench's answer was useful as a starting point which is why I accepted it as that answer despite the following caveats: Potench 的回答作为一个起点很有用,这就是为什么尽管有以下警告,我还是接受了它作为那个答案:

  • It only works in Webkit browsers.它只适用于 Webkit 浏览器。 :-) :-)
  • As originally defined it does not work since video.webkitDisplayingFullscreen is true whenever the resize event fires.正如最初定义的那样,它不起作用,因为只要resize事件触发, video.webkitDisplayingFullscreen就为true

I got this to work - only on Webkit browsers - by tapping into animation frames to constantly watch for the change in value:我让这个工作 - 只在 Webkit 浏览器上 - 通过点击动画帧来不断观察值的变化:

var onFrame, isVideoFullscreen;

onFrame = window.requestAnimationFrame ||
          window.mozRequestAnimationFrame ||
          window.webkitRequestAnimationFrame ||
          window.oRequestAnimationFrame ||
          window.msRequestAnimationFrame ||
          function (fn) {
              setTimeout(fn, 1000 / 60);
          };

isVideoFullscreen = false;

function frame() {
    if (!isVideoFullscreen && video.webkitDisplayingFullscreen) {
        // entered fullscreen mode
        isVideoFullscreen = true;
    } else if (isVideoFullscreen && !video.webkitDisplayingFullscreen) {
        // exited fullscreen mode
        isVideoFullscreen = false;
    }
    onFrame(frame);
}
onFrame(frame);

Ok I think I have a solution for you... I'm just going to assume you use jQuery for ease of writing this code.好吧,我想我有一个适合你的解决方案......我只是假设你使用 jQuery 来简化编写这段代码。

I don't believe you'll be able to capture keyboard events while in Fullscreen mode... but you could do this to get notified when you enter or exit fullscreen mode.我不相信在全屏模式下您将无法捕获键盘事件……但是您可以这样做以便在进入或退出全屏模式时得到通知。

var isVideoFullscreen = video.webkitDisplayingFullscreen;

$(window).bind("resize", function (e) {
    // check to see if your browser has exited fullscreen
    if (isVideoFullscreen != video.webkitDisplayingFullscreen) { // video fullscreen mode has changed
       isVideoFullscreen =  video.webkitDisplayingFullscreen;

       if (isVideoFullscreen) {
            // you have just ENTERED full screen video
       } else {
            // you have just EXITED full screen video
       }
    }
});

Hope this helps or points you in the right direction希望这有助于或指向正确的方向

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM