gpt4 book ai didi

javascript - 如何检测退格键是否被保留

转载 作者:行者123 更新时间:2023-11-30 11:02:08 25 4
gpt4 key购买 nike

我如何检测退格键是否被按下并在 js 中保持。我设置了事件监听器,但每次按下退格键时它都会触发我需要区分按下和按住吗?

HTML 输入:

(keydown)="fixIePosition($event);

TS 回调:

fixIePosition(e) {

if ((navigator.userAgent.indexOf('Trident') !== -1 ||
navigator.userAgent.indexOf('Edge') !== -1)
&& e.keyCode === 8) {
const selectionStart = e.target.selectionStart;

if (selectionStart === 3) {
e.target.setSelectionRange(3, 3);
// } else if (selectionStart === 5 && e.type === 'keydown') {
// console.log(e);
// // e.target.setSelectionRange(selectionStart, selectionStart);
// this.ieFixPosition = true;
} else if (selectionStart === 4 && e.type === 'keyup') {
if (this.ieFixPosition) {
e.target.setSelectionRange(selectionStart, selectionStart);
this.ieFixPosition = false;
} else {
e.target.setSelectionRange(selectionStart - 1, selectionStart - 1);
}
}
}
}

最佳答案

你可以试试键盘事件repeat检测按键保持。

document.addEventListener('keydown', (event) => {
if (event.repeat) {
// Key Hold Event
} else {
// Key Press Event
}
});

关于javascript - 如何检测退格键是否被保留,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57274584/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com