Add Shift tap to force English for the next word
Pressing and releasing Shift on its own arms literal mode for the next word, so lowercase English can be typed without it being read as Zhuyin; it resets after the word is committed and a second tap cancels it. While typing, a Shift tap flips the buffer like Tab. Forcing before typing is not recorded as a learned correction; flipping is. Mac: listens for flagsChanged events. Windows: Shift key-up via PIME's filterKeyUp/onKeyUp, with a short "英文" tooltip. The tap detection lives in the core (ShiftTapDetector / shift_tap.py) with tests on both sides. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -34,15 +34,32 @@ enum Shared {
|
||||
@objc(AutoZhuyinInputController)
|
||||
final class InputController: IMKInputController {
|
||||
private let engine = CompositionEngine(classifier: Shared.classifier)
|
||||
private var shiftTap = ShiftTapDetector()
|
||||
|
||||
// MARK: - Key events
|
||||
|
||||
override func recognizedEvents(_ sender: Any!) -> Int {
|
||||
Int(NSEvent.EventTypeMask.keyDown.rawValue)
|
||||
Int(NSEvent.EventTypeMask.keyDown.rawValue | NSEvent.EventTypeMask.flagsChanged.rawValue)
|
||||
}
|
||||
|
||||
override func handle(_ event: NSEvent!, client sender: Any!) -> Bool {
|
||||
guard let event, event.type == .keyDown, let client = sender as? IMKTextInput else { return false }
|
||||
guard let event, let client = sender as? IMKTextInput else { return false }
|
||||
|
||||
// Shift pressed and released alone: next word is English (or flip the current one).
|
||||
if event.type == .flagsChanged {
|
||||
let flags = event.modifierFlags
|
||||
let tapped = shiftTap.modifiersChanged(
|
||||
shift: flags.contains(.shift),
|
||||
otherModifiers: !flags.intersection([.command, .control, .option, .function]).isEmpty,
|
||||
time: event.timestamp)
|
||||
if tapped {
|
||||
_ = engine.handle(.shiftTap)
|
||||
updateComposition(client)
|
||||
}
|
||||
return false
|
||||
}
|
||||
guard event.type == .keyDown else { return false }
|
||||
shiftTap.keyPressed()
|
||||
|
||||
// Shortcuts (⌘C, ⌃A, ⌥…) go to the app; commit any composition first.
|
||||
if !event.modifierFlags.intersection([.command, .control, .option]).isEmpty {
|
||||
|
||||
@@ -4,6 +4,8 @@ public enum KeyInput: Equatable {
|
||||
case character(Character)
|
||||
case backspace, enter, escape, tab
|
||||
case up, down, left, right
|
||||
/// Shift pressed and released on its own.
|
||||
case shiftTap
|
||||
}
|
||||
|
||||
public struct KeyResult: Equatable {
|
||||
@@ -33,8 +35,12 @@ public final class CompositionEngine {
|
||||
public let converter: Converter
|
||||
|
||||
public private(set) var keys: [Character] = []
|
||||
/// Set by Tab: overrides the classifier for the whole buffer.
|
||||
/// Set by Tab / Shift tap: overrides the classifier for the whole buffer.
|
||||
public private(set) var forcedMode: InputMode?
|
||||
/// True when forcedMode corrects the classifier (learned on commit); false when the
|
||||
/// user forced English before typing (Shift tap on an empty buffer), which is not a
|
||||
/// correction and is not learned.
|
||||
private var forcedIsCorrection = false
|
||||
/// User-picked phrases: Zhuyin segment start (key index) → syllable range → text.
|
||||
private var fixed: [Int: [Range<Int>: String]] = [:]
|
||||
public private(set) var candidates: [Candidate] = []
|
||||
@@ -108,7 +114,11 @@ public final class CompositionEngine {
|
||||
guard !keys.isEmpty else { return .passThrough }
|
||||
keys.removeLast()
|
||||
fixed = fixed.filter { $0.key < keys.count }
|
||||
if keys.isEmpty { forcedMode = nil; fixed = [:] }
|
||||
if keys.isEmpty {
|
||||
// A Shift-tap "next word is English" stays armed; a correction is dropped.
|
||||
if forcedIsCorrection { forcedMode = nil; forcedIsCorrection = false }
|
||||
fixed = [:]
|
||||
}
|
||||
return .consumed
|
||||
case .enter:
|
||||
guard !keys.isEmpty else { return .passThrough }
|
||||
@@ -119,7 +129,16 @@ public final class CompositionEngine {
|
||||
return .consumed
|
||||
case .tab:
|
||||
guard !keys.isEmpty else { return .passThrough }
|
||||
forcedMode = (mode == .zhuyin) ? .literal : .zhuyin
|
||||
flip()
|
||||
return .consumed
|
||||
case .shiftTap:
|
||||
if keys.isEmpty {
|
||||
// Next word is English, typed as-is (lowercase stays lowercase). Tap again to cancel.
|
||||
forcedMode = forcedMode == .literal ? nil : .literal
|
||||
forcedIsCorrection = false
|
||||
} else {
|
||||
flip()
|
||||
}
|
||||
return .consumed
|
||||
case .down:
|
||||
guard !keys.isEmpty else { return .passThrough }
|
||||
@@ -130,6 +149,15 @@ public final class CompositionEngine {
|
||||
}
|
||||
}
|
||||
|
||||
/// Forces the whole buffer into the other mode (a correction that is learned).
|
||||
private func flip() {
|
||||
forcedMode = (mode == .zhuyin) ? .literal : .zhuyin
|
||||
forcedIsCorrection = true
|
||||
}
|
||||
|
||||
/// True while the user has forced English before typing anything (Shift tap).
|
||||
public var isEnglishArmed: Bool { keys.isEmpty && forcedMode == .literal }
|
||||
|
||||
private func handleCharacter(_ c: Character) -> KeyResult {
|
||||
if c == " " {
|
||||
guard !keys.isEmpty else { return .passThrough }
|
||||
@@ -224,7 +252,7 @@ public final class CompositionEngine {
|
||||
private func commitCurrent(_ given: [Segment]? = nil) -> String {
|
||||
let segs = given ?? segments
|
||||
let text = segs.map { render($0, final: true) }.joined()
|
||||
if let forced = forcedMode {
|
||||
if let forced = forcedMode, forcedIsCorrection {
|
||||
classifier.learned.record(String(keys), as: forced)
|
||||
}
|
||||
previousMode = segs.last?.mode
|
||||
@@ -241,6 +269,7 @@ public final class CompositionEngine {
|
||||
public func reset() {
|
||||
keys = []
|
||||
forcedMode = nil
|
||||
forcedIsCorrection = false
|
||||
fixed = [:]
|
||||
candidates = []
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
import Foundation
|
||||
|
||||
/// Detects Shift pressed and released on its own (no other key in between, released
|
||||
/// within `maxDuration`), as opposed to Shift used for capitals or shortcuts.
|
||||
///
|
||||
/// Python twin: windows/autozhuyin/core/shift_tap.py
|
||||
public struct ShiftTapDetector {
|
||||
public var maxDuration: TimeInterval = 0.5
|
||||
private var pressedAt: TimeInterval?
|
||||
private var used = false
|
||||
|
||||
public init() {}
|
||||
|
||||
/// Call when the modifier state changes. Returns true when a Shift tap completed.
|
||||
public mutating func modifiersChanged(shift: Bool, otherModifiers: Bool, time: TimeInterval) -> Bool {
|
||||
if shift && !otherModifiers {
|
||||
if pressedAt == nil {
|
||||
pressedAt = time
|
||||
used = false
|
||||
}
|
||||
return false
|
||||
}
|
||||
defer { pressedAt = nil }
|
||||
guard let start = pressedAt, !shift, !otherModifiers, !used else { return false }
|
||||
return time - start <= maxDuration
|
||||
}
|
||||
|
||||
/// Call for every other key press: Shift was used as a modifier, not tapped.
|
||||
public mutating func keyPressed() {
|
||||
if pressedAt != nil { used = true }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user