<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
</body>
<script>

let longPressKey = {
timeout: null,
trigger(
pressedEndCallback, //长按结束触发方法
pressedStartCallback, //按下触发方法
pressedUpCallback, //弹起触发方法
keyCode = 19, //长按什么键
delaySecond = 3 //长按多少秒触发
) {
onkeydown = (e) => {
let k = e.keyCode || e.which || e.charCode,
ck = e.ctrlKey || e.metaKey,
ak = e.altKey,
sk = e.shiftKey;
if (k === keyCode) {
if (!this.timeout) {
pressedStartCallback && pressedStartCallback(e);
this.timeout = setTimeout(() => {
this.timeout = null;
pressedEndCallback && pressedEndCallback(e);
}, 1000 * delaySecond);
}
}
};
onkeyup = (e) => {
clearTimeout(this.timeout);
this.timeout = null;
pressedUpCallback && pressedUpCallback(e);
};
},
};

//测试用例----------------------------------------
let versionTime = '2022年2月6日 17:43:12';
longPressKey.trigger(
(e) => {
// 长按超时执行
alert(`【更新时间】${versionTime}\n(注意校对是否已发布最新代码)`);
},
(e) => {
// 刚刚按下键
console.log("按键对象",e);
},
(e) => {
// 弹起
console.log("弹起键对象",e);
},
19,
3
);

</script>
</html>