求解如何屏蔽网页原有快捷键
这个长按0键3倍速的脚本如下,只能在和原网页快捷键不冲突的情况下实现,假如键值为arrowright就会和播放器的长按一直快进5秒冲突,不知道怎么实现先屏蔽网页原有的快捷键设置,然后单击arrowright快进5秒,长按3倍速度,松手1倍速度,和b站web逻辑一样 (function() {'use strict';
var speed=1;
var a_speed=3;
document.onkeydown=function(e){//对整个页面监听
var keyNum=window.event ? e.keyCode :e.which;//获取被按下的键值
if(keyNum==96){
if(document.querySelector('video')){
if(document.querySelector('video').playbackRate!=a_speed){
speed=document.querySelector('video').playbackRate;
}
document.querySelector('video').playbackRate = a_speed;
}
}
}
document.onkeyup=function(e){//对整个页面监听
var keyNum=window.event ? e.keyCode :e.which;//获取被按下的键值
if(keyNum==96){
if(document.querySelector('video')){
document.querySelector('video').playbackRate = speed;
}
}
} 在事件捕获阶段stopImmediatePropagation:https://developer.mozilla.org/zh-CN/docs/Web/API/Event/stopImmediatePropagation
let holding = 0;
window.addEventListener('keydown', e => {
const video = document.querySelector('video');
if (e.key === 'ArrowRight' && video) {
e.stopImmediatePropagation();
holding++;
if (holding > 1) {
video.playbackRate = 3;
}
}
}, true);
window.addEventListener('keyup', e => {
const video = document.querySelector('video');
if (e.key === 'ArrowRight' && video) {
e.stopImmediatePropagation();
if (holding > 1) {
video.playbackRate = 1;
} else {
video.currentTime += 5;
}
holding = 0;
}
}, true); cxxjackie 发表于 2023-1-2 12:56
在事件捕获阶段stopImmediatePropagation:https://developer.mozilla.org/zh-CN/docs/Web/API/Event/stopI ...
感谢大佬
页:
[1]