JavaScript检测播放/暂停键盘(虚拟)键 [英] JavaScript detecting play/pause keyboard (virtual) key

查看:60
本文介绍了JavaScript检测播放/暂停键盘(虚拟)键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

偶然地,我只是按下了键盘上的播放/暂停(▶/❚❚)按钮(,同时在未聚焦的标签页中播放YouTube视频.令我惊讶的是,YouTube视频立即暂停了.

现在,我尝试查找它的工作原理,但是我无法在互联网上找到任何内容来解释如何检测到像这样的按键.我尝试在控制台中运行 onkeydown = function(e){console.log(e)} ,但是按播放/暂停按钮不会触发任何事件.另外, https://keycode.info/对此也没有提供任何帮助.我确实找到了 http://www.kbdedit.com/manual/low_level_vk_list.html 其中列出了很多虚拟键代码",其中确实包括 VK_MEDIA_PLAY_PAUSE ,这可能是我所按下的键,但是我没有找到任何方法来触发JS事件.

现在,我确实想指定此功能似乎不适用于Firefox,仅适用于Chrome(据我测试).可能它仍处于试验阶段,但我非常想知道YouTube使用了什么系统来捕获此事件,即使当前未打开标签页(Chrome当时甚至没有聚焦)

PS:我在Ubuntu 18.04上经历过;例如,我不确定是否可以在Windows上使用.

解决方案

Chrome对此负责(硬件媒体密钥处理),有关更多详细信息,请查看chrome://flags/#hardware-media-key-handling

这里还有包含文档和演示的链接: https://www.chromestatus.com/feature/5639924124483584

By accident, I just pressed the play/pause (▶/❚❚) button on my keyboard (the button just above Num Lock on , while playing a YouTube video in a tab that was not focused. To my massive surprise, the YouTube video paused immediately.

Now, I tried looking up how this works, but I was not able to find anything on the internet explaining how a keypress can be detected for keys like this. I tried running onkeydown = function(e) {console.log(e)} in my console, but pressing the play/pause button did not trigger any event. Also, https://keycode.info/ did not give me any help with this either. I did find http://www.kbdedit.com/manual/low_level_vk_list.html which lists a whole lot of 'virtual key codes', which does include VK_MEDIA_PLAY_PAUSE, which is probably the key I am pressing, but I did not find any way to trigger an event in JS with this.

Now I do want to specify that this functionality does not seem to work in Firefox, only in Chrome (as far as I've tested). It might be something that's still experimental, but I am really interested to hear what system YouTube uses to capture this event, even when the tab is currently not opened (Chrome wasn't even focused at the moment)

PS: I experienced this on Ubuntu 18.04; I'm not sure if this will work on Windows, for example.

解决方案

Chrome is responsible for that (Hardware Media Key Handling), for more details check out chrome://flags/#hardware-media-key-handling

Also here is link that contains docs and demo: https://www.chromestatus.com/feature/5639924124483584

这篇关于JavaScript检测播放/暂停键盘(虚拟)键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆