我做了一些深入的研究,到目前为止,我发现通常在输入字段中写入值时,该字段会遇到以下JavaScript事件(按以下顺序):
“按键”
“按键”
“键”
..,但是当macOS自动用句点替换双倍空格时,这种情况发生在输入字段中的文本紧跟着连续两次在空格键上连续敲击两次(即,输入字段不能为空)时, “ keypress”事件不会触发,只会发生以下事件:
“按键”
“键”
这意味着,即使这可能是一个可怕的解决方案,也可以检测到更改,例如通过监视最后一个action并在keyup事件中采取措施(如果前一个事件不是keypress:)
macOS prevent period/full stop on double-spacevar input = document.getElementsByTagName("input")[0],
action;
input.addEventListener("keydown", function (e){
var info = {
code: e.code,
keyCode: e.keyCode
};
//Open the key-event
console.group("key event start", info);
console.group("keydown");
console.log({
e: e
});
action = e.type;
console.groupEnd();
});
input.addEventListener("keypress", function (e){
console.group("keypress");
console.log({
e: e
});
action = e.type;
// console.log("value", e.target.value);
console.groupEnd();
});
input.addEventListener("keyup", function (e){
console.group("keyup");
console.log({
e: e
});
if (action != "keypress" && e.keyCode == 32 && e.target.value.endsWith(". ")) {
console.warn("keypress action has been skipped, space-bar has been pressed and the input ends with '. '");
e.target.value = e.target.value.replace(new RegExp(". " + "$"), " ");
}
console.groupEnd();
//Close key-event
console.groupEnd();
});