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 <noreply@anthropic.com>
This commit is contained in:
2026-11-11 11:35:59 +08:00
co-authored by Claude Opus 5.5
parent 82f20a6551
commit e928d08fdc
6 changed files with 101 additions and 12 deletions
+2 -2
View File
@@ -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` = 檔) |
+28 -5
View File
@@ -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<Character> = ["'", ".", ",", "-", "/", ";"]
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)
@@ -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)
}
}
+25 -4
View File
@@ -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
+2 -1
View File
@@ -29,7 +29,8 @@
← → 在組字區移動游標,選要改哪個字
↓ 開啟游標前那個字或詞的候選字,按數字鍵 1–9 選字
Esc 取消組字
單按 Shift 下一個字強制英文(保持小寫),送出後恢復自動判斷;
單按 Shift 下一個字強制英文(保持小寫、直接送出、沒有底線),
按空白或 Enter 後恢復自動判斷;
打到一半時按,同 Tab 整段翻轉。會出現「英文」小提示
Shift+字母 打大寫字母(也會強制英文)
< > ? ! : 中文語境下自動變成全形 ,。?!:
+20
View File
@@ -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()