From c92f3e82ac9bc8981358fa47855ecf76d91c1e5d Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Fri, 5 Aug 2022 15:44:01 -0700 Subject: [PATCH] Fix IME input --- nicolator.js | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/nicolator.js b/nicolator.js index 0f8a14e..d86d79f 100644 --- a/nicolator.js +++ b/nicolator.js @@ -76,8 +76,7 @@ function convertKeyCode(evt) { if (keyCode == 88) chara = (shift) ? "X" : "x"; if (keyCode == 89) - chara = (shift) ? "Y" : "y"; - if (keyCode == 90) + chara = (shift) ? "Y" : "y"; if (keyCode == 90) chara = (shift) ? "Z" : "z"; if (keyCode == 93) chara = "select key"; @@ -114,7 +113,6 @@ const leftShiftCode = 32; // space const rightShiftCode = 16; // shift key let leftShift = false; let rightShift = false; -let inComposition = false; addEventListener("keydown", event => { switch(event.keyCode) { case leftShiftCode: @@ -137,12 +135,19 @@ addEventListener("keyup", event => { break; } }); -addEventListener("compositionstart", event => inComposition = true); -addEventListener("compositionend", event => inComposition = false); -input.addEventListener("keydown", e => { - if (inComposition) { - return; +const check = () => { + if (input.value == word) { + clearTimeout(timeout); + wordDisplay.style.color = "green"; + 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(); if (keymap[pressed] !== undefined) { let valueIndex = 0; @@ -152,15 +157,7 @@ input.addEventListener("keydown", e => { valueIndex = 2; } input.value += keymap[pressed][valueIndex]; - if (input.value == word) { - clearTimeout(timeout); - wordDisplay.style.color = "green"; - wordDisplay.innerHTML += " ✅"; - setTimeout(() => { - wordDisplay.style.color = ""; - reset(); - }, 1000); - } + check(); e.preventDefault(); } else if (pressed == " ") { e.preventDefault();