Fix IME input

main
Elnu 2 years ago
parent d0fa263a38
commit c92f3e82ac

@ -76,8 +76,7 @@ function convertKeyCode(evt) {
if (keyCode == 88) if (keyCode == 88)
chara = (shift) ? "X" : "x"; chara = (shift) ? "X" : "x";
if (keyCode == 89) if (keyCode == 89)
chara = (shift) ? "Y" : "y"; chara = (shift) ? "Y" : "y"; if (keyCode == 90)
if (keyCode == 90)
chara = (shift) ? "Z" : "z"; chara = (shift) ? "Z" : "z";
if (keyCode == 93) if (keyCode == 93)
chara = "select key"; chara = "select key";
@ -114,7 +113,6 @@ const leftShiftCode = 32; // space
const rightShiftCode = 16; // shift key const rightShiftCode = 16; // shift key
let leftShift = false; let leftShift = false;
let rightShift = false; let rightShift = false;
let inComposition = false;
addEventListener("keydown", event => { addEventListener("keydown", event => {
switch(event.keyCode) { switch(event.keyCode) {
case leftShiftCode: case leftShiftCode:
@ -137,12 +135,19 @@ addEventListener("keyup", event => {
break; break;
} }
}); });
addEventListener("compositionstart", event => inComposition = true); const check = () => {
addEventListener("compositionend", event => inComposition = false); if (input.value == word) {
input.addEventListener("keydown", e => { clearTimeout(timeout);
if (inComposition) { wordDisplay.style.color = "green";
return; wordDisplay.innerHTML += " ✅";
setTimeout(() => {
wordDisplay.style.color = "";
reset();
}, 1000);
} }
};
addEventListener("compositionend", event => check());
input.addEventListener("keydown", e => {
const pressed = convertKeyCode(e).toLowerCase(); // String.fromCharCode(e.keyCode).toLowerCase(); const pressed = convertKeyCode(e).toLowerCase(); // String.fromCharCode(e.keyCode).toLowerCase();
if (keymap[pressed] !== undefined) { if (keymap[pressed] !== undefined) {
let valueIndex = 0; let valueIndex = 0;
@ -152,15 +157,7 @@ input.addEventListener("keydown", e => {
valueIndex = 2; valueIndex = 2;
} }
input.value += keymap[pressed][valueIndex]; input.value += keymap[pressed][valueIndex];
if (input.value == word) { check();
clearTimeout(timeout);
wordDisplay.style.color = "green";
wordDisplay.innerHTML += " ✅";
setTimeout(() => {
wordDisplay.style.color = "";
reset();
}, 1000);
}
e.preventDefault(); e.preventDefault();
} else if (pressed == " ") { } else if (pressed == " ") {
e.preventDefault(); e.preventDefault();