From e928d08fdc69086d9dfd44f9d45bf78a7ee3bdd3 Mon Sep 17 00:00:00 2001 From: John Kao Date: Wed, 11 Nov 2026 11:35:59 +0800 Subject: [PATCH] Type forced English directly, without an underlined composition After a Shift tap, letters, digits and word punctuation go straight to the app instead of into marked text, so nothing is underlined. Space, Enter, Tab, Escape or another symbol ends the forced word and automatic detection resumes; backspace is handled by the app. Co-Authored-By: Claude Opus 5.5 --- README.md | 4 +-- .../AutoZhuyinCore/CompositionEngine.swift | 33 ++++++++++++++++--- .../AutoZhuyinCoreTests.swift | 24 ++++++++++++++ windows/autozhuyin/core/engine.py | 29 +++++++++++++--- windows/installer/README.txt | 3 +- windows/tests/test_core.py | 20 +++++++++++ 6 files changed, 101 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 239eeb1..573f9af 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ - **中英自動判斷**:每按一個鍵就重新判斷,組字區即時顯示結果(Mac 版英文以藍字顯示) - **中英混打不用空白**:`ji3hello` → 我hello、`hellosu3cl3` → hello你好,同一段內會自動切分 - **判斷錯了按 Tab**:在中/英之間翻轉,輸入法會記住這組按鍵,下次自動判斷對 -- **單按 Shift 強制英文**:要打英文時先單按一下 Shift,接下來這個字照打、保持小寫 +- **單按 Shift 強制英文**:要打英文時先單按一下 Shift,接下來這個字照打、保持小寫,而且直接送出、不顯示底線 - **注音順序打錯也能修正**:`ㄧㄒㄠˇ`、`ㄒㄠㄧˇ` 都會變成 `ㄒㄧㄠˇ`(小) - **可省略聲調**:`jiap7`(ㄨㄛ ㄇㄣ˙)一樣能打出「我們」 - **候選字**:用 ← → 把游標移到要改的字後面,按 ↓ 選字;選過的會固定下來 @@ -54,7 +54,7 @@ SmartScreen 出現警告時,按「其他資訊 → 仍要執行」。解除安 | ← → | 在組字區移動游標,選要改哪個字 | | ↓ | 開啟游標前那個字或詞的候選字(游標預設在最後),數字鍵 1–9 選字,Esc 關閉 | | Esc | 取消組字 | -| **單按 Shift** | 沒在打字時:下一個字強制英文(保持小寫),送出後恢復自動判斷,再按一次取消。打到一半時:同 Tab,整段翻轉 | +| **單按 Shift** | 沒在打字時:下一個字強制英文(保持小寫),字母直接送出、沒有底線;按空白、Enter 或其他符號後恢復自動判斷,再按一次 Shift 取消。打到一半時:同 Tab,整段翻轉 | | Shift + 字母 | 打大寫字母(也會強制英文) | | `< > ? ! : ( ) [ ]` | 中文語境下自動轉成全形 `,。?!:()「」` | | `, . / ; -` | 英文單字後是標點;在注音中是 ㄝ ㄡ ㄥ ㄤ ㄦ(例如 `2;3` = 檔) | diff --git a/Sources/AutoZhuyinCore/CompositionEngine.swift b/Sources/AutoZhuyinCore/CompositionEngine.swift index e08f57d..ab9c0d4 100644 --- a/Sources/AutoZhuyinCore/CompositionEngine.swift +++ b/Sources/AutoZhuyinCore/CompositionEngine.swift @@ -172,14 +172,14 @@ public final class CompositionEngine { } return .consumed case .enter: - guard !keys.isEmpty else { return .passThrough } + guard !keys.isEmpty else { endDirectEnglish(); return .passThrough } return KeyResult(handled: true, commit: commitCurrent()) case .escape: - guard !keys.isEmpty else { return .passThrough } + guard !keys.isEmpty else { endDirectEnglish(); return .passThrough } reset() return .consumed case .tab: - guard !keys.isEmpty else { return .passThrough } + guard !keys.isEmpty else { endDirectEnglish(); return .passThrough } flip() return .consumed case .shiftTap: @@ -214,11 +214,34 @@ public final class CompositionEngine { forcedIsCorrection = true } - /// True while the user has forced English before typing anything (Shift tap). - public var isEnglishArmed: Bool { keys.isEmpty && forcedMode == .literal } + /// True while the user has forced English for the next word (Shift tap). In this + /// mode keys go straight to the app with no composition (no underline). + public var isEnglishArmed: Bool { keys.isEmpty && forcedMode == .literal && !forcedIsCorrection } + + /// Leaves forced-English mode once the word is done. + private func endDirectEnglish() { + guard isEnglishArmed else { return } + forcedMode = nil + previousMode = .literal + previousCommitTime = now() + } + + private static let englishWordPunctuation: Set = ["'", ".", ",", "-", "/", ";"] private func handleCharacter(_ c: Character) -> KeyResult { cursor = nil // typing always continues at the end + if isEnglishArmed { + // Forced English: insert directly. Space ends the word; other symbols end it too. + if c == " " { + endDirectEnglish() + return KeyResult(handled: true, commit: " ") + } + if c.isASCII && (c.isLetter || c.isNumber || CompositionEngine.englishWordPunctuation.contains(c)) { + return KeyResult(handled: true, commit: String(c)) + } + endDirectEnglish() + return .passThrough + } if c == " " { guard !keys.isEmpty else { return .passThrough } let segs = segments(of: keys, finishing: true) diff --git a/Tests/AutoZhuyinCoreTests/AutoZhuyinCoreTests.swift b/Tests/AutoZhuyinCoreTests/AutoZhuyinCoreTests.swift index 6a19657..c5b0864 100644 --- a/Tests/AutoZhuyinCoreTests/AutoZhuyinCoreTests.swift +++ b/Tests/AutoZhuyinCoreTests/AutoZhuyinCoreTests.swift @@ -405,3 +405,27 @@ final class CursorTests: XCTestCase { XCTAssertTrue(e.isShowingCandidates) } } + +final class DirectEnglishTests: XCTestCase { + func testForcedEnglishHasNoComposition() { + let e = CompositionEngine(classifier: Classifier(lexicon: sharedLexicon, english: sharedEnglish, weights: sharedWeights)) + _ = e.handle(.shiftTap) + let r = e.handle(.character("r")) + XCTAssertEqual(r.commit, "r") // inserted right away + XCTAssertEqual(e.markedText, "") // nothing underlined + XCTAssertEqual(e.handle(.character("u")).commit, "u") + XCTAssertEqual(e.handle(.character("'")).commit, "'") + XCTAssertFalse(e.handle(.backspace).handled) // the app deletes it + XCTAssertTrue(e.isEnglishArmed) + XCTAssertEqual(e.handle(.character(" ")).commit, " ") + XCTAssertFalse(e.isEnglishArmed) // back to automatic + } + + func testEnterEndsForcedEnglish() { + let e = CompositionEngine(classifier: Classifier(lexicon: sharedLexicon, english: sharedEnglish, weights: sharedWeights)) + _ = e.handle(.shiftTap) + _ = e.handle(.character("a")) + XCTAssertFalse(e.handle(.enter).handled) + XCTAssertFalse(e.isEnglishArmed) + } +} diff --git a/windows/autozhuyin/core/engine.py b/windows/autozhuyin/core/engine.py index b73aef1..a4a3d3a 100644 --- a/windows/autozhuyin/core/engine.py +++ b/windows/autozhuyin/core/engine.py @@ -152,8 +152,15 @@ class CompositionEngine: @property def english_armed(self): - """True while the user has forced English before typing anything (Shift tap).""" - return not self.keys and self.forced_mode == LITERAL + """True while the user has forced English for the next word (Shift tap). In this + mode keys go straight to the app with no composition (no underline).""" + return not self.keys and self.forced_mode == LITERAL and not self._forced_is_correction + + def _end_direct_english(self): + if self.english_armed: + self.forced_mode = None + self._previous_mode = LITERAL + self._previous_time = self.now() def _flip(self): self.forced_mode = LITERAL if self.mode == ZHUYIN else ZHUYIN @@ -161,7 +168,7 @@ class CompositionEngine: def wants(self, key): """Whether handle(key) would consume the key, without changing state.""" - if self.candidates or key == SHIFT_TAP: + if self.candidates or key == SHIFT_TAP or self.english_armed: return True if key in SPECIAL_KEYS: return bool(self.keys) @@ -190,14 +197,19 @@ class CompositionEngine: self.fixed = {} return CONSUMED if key == ENTER: - return (True, self._commit()) if self.keys else PASS + if not self.keys: + self._end_direct_english() + return PASS + return True, self._commit() if key == ESCAPE: if not self.keys: + self._end_direct_english() return PASS self.reset() return CONSUMED if key == TAB: if not self.keys: + self._end_direct_english() return PASS self._flip() return CONSUMED @@ -231,6 +243,15 @@ class CompositionEngine: def _handle_character(self, c): self.cursor = None # typing always continues at the end + if self.english_armed: + # Forced English: insert directly. Space ends the word; other symbols end it too. + if c == " ": + self._end_direct_english() + return True, " " + if c.isascii() and (c.isalnum() or c in "'.,-/;"): + return True, c + self._end_direct_english() + return PASS if c == " ": if not self.keys: return PASS diff --git a/windows/installer/README.txt b/windows/installer/README.txt index 10f52ed..fe0a58f 100644 --- a/windows/installer/README.txt +++ b/windows/installer/README.txt @@ -29,7 +29,8 @@ ← → 在組字區移動游標,選要改哪個字 ↓ 開啟游標前那個字或詞的候選字,按數字鍵 1–9 選字 Esc 取消組字 - 單按 Shift 下一個字強制英文(保持小寫),送出後恢復自動判斷; + 單按 Shift 下一個字強制英文(保持小寫、直接送出、沒有底線), + 按空白或 Enter 後恢復自動判斷; 打到一半時按,同 Tab 整段翻轉。會出現「英文」小提示 Shift+字母 打大寫字母(也會強制英文) < > ? ! : 中文語境下自動變成全形 ,。?!: diff --git a/windows/tests/test_core.py b/windows/tests/test_core.py index d6b78ae..377802b 100644 --- a/windows/tests/test_core.py +++ b/windows/tests/test_core.py @@ -267,5 +267,25 @@ class CursorTests(unittest.TestCase): self.assertTrue(e.showing_candidates) +class DirectEnglishTests(unittest.TestCase): + def test_forced_english_has_no_composition(self): + e = make_engine() + e.handle(SHIFT_TAP) + self.assertEqual(e.handle("r"), (True, "r")) + self.assertEqual(e.marked_text, "") + self.assertEqual(e.handle("'"), (True, "'")) + self.assertEqual(e.handle(BACKSPACE), (False, None)) + self.assertTrue(e.english_armed) + self.assertEqual(e.handle(" "), (True, " ")) + self.assertFalse(e.english_armed) + + def test_enter_ends_forced_english(self): + e = make_engine() + e.handle(SHIFT_TAP) + e.handle("a") + self.assertEqual(e.handle(ENTER), (False, None)) + self.assertFalse(e.english_armed) + + if __name__ == "__main__": unittest.main()