简体   繁体   English

“按键”事件开启<video>标签未触发箭头键</video>

[英]“keypress” event on <video> tag not firing for arrow keys

So I wanted to disable the arrow keys shortcuts for default browser video player.所以我想禁用默认浏览器视频播放器的箭头键快捷方式。 I figured it looks like it's using "keypress" event, so I should be able to do it with simple我认为它看起来像是在使用“keypress”事件,所以我应该能够用简单的方法来做到这一点

player.addEventListener("keypress", (e) => {
            e.preventDefault();
            e.stopPropagation();
        });

however it didn't work, because as I found out the listener weren't firing when an arrow key was pressed, it does work for all other keys.但是它不起作用,因为当我发现按下箭头键时侦听器没有触发,它对所有其他键都有效。 I added a console.log instead of preventDefault inside listener func, and when smashing random keys on my keyboard I was getting the log as expected, but for arrow keys there was nothing.我在侦听器函数中添加了一个 console.log 而不是 preventDefault ,当我在键盘上敲击随机键时,我得到了预期的日志,但是对于箭头键,什么都没有。

With "keydown" event listeners fires, but preventDefault does nothing, arrow keys still work.随着“keydown”事件监听器触发,但 preventDefault 什么都不做,箭头键仍然有效。

Why is that?这是为什么? And is there any way to disable those shortcuts and make "keypress" event listener for arrow keys work?有没有办法禁用这些快捷方式并使箭头键的“keypress”事件监听器工作?

After a few failed attempts I remembered that I too had the same problem and that I solved it thanks to a post by KayaNatsumi 's on html5 video behavior control.经过几次失败的尝试后,我记得我也遇到了同样的问题,并且感谢KayaNatsumi在 html5 视频行为控制上的帖子,我解决了这个问题。

Link to question: https://stackoverflow.com/a/63040542/14124576问题链接: https://stackoverflow.com/a/63040542/14124576

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

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