oficina / sheets Public

Mac: a layout's own dead key (Spanish ´, German ^) starts typing too

Review #72 finding 4. startsTyping needed a character from characters or
charactersIgnoringModifiers; a dead key that needs no Option may carry
neither, so ´ then e on a Spanish layout would still have given "e". A
key-down with no characters, no special key and no Command or Control is
now a dead key: modifier keys arrive as flagsChanged and every other
non-text key is special.

testLayoutDeadKeysWithoutOptionStartTyping reads the Spanish-ISO and German
layouts with UCKeyTranslate (without selecting them, so the machine's
keyboard is untouched), finds their unmodified dead keys, checks each
composes with e, and checks such a key-down starts typing whether AppKit
leaves charactersIgnoringModifiers empty or fills it; it fails without
this. A real keyboard try is still worth doing: synthetic events do not
pass through the layout.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev 7ad778d4f863943cb2c39f4f099ee09919f0799f parent 7088fc3
2 files changed, +46 −1
  • native/apple/Sources/SheetsKit/SheetTypeAhead.swift +5 −1
  • native/apple/Tests/SheetsKitTests/FirstKeystrokeTests.swift +41 −0
diff --git a/native/apple/Sources/SheetsKit/SheetTypeAhead.swift b/native/apple/Sources/SheetsKit/SheetTypeAhead.swift
index d6c18af..2c1ad84 100644
--- a/native/apple/Sources/SheetsKit/SheetTypeAhead.swift
+++ b/native/apple/Sources/SheetsKit/SheetTypeAhead.swift
@@ -31,8 +31,12 @@ import SwiftUI
event.specialKey == nil
else { return false }
// A dead key has no characters yet; what it will compose is still text.
+ // With Option (US ´ is Option-E) the unmodified key still names a
+ // character; a layout's own dead key (Spanish ´, German ^, French ^) may
+ // name none at all, and that empty key-down is a dead key too: modifier
+ // keys arrive as flagsChanged, and every other non-text key is special.
let text = (event.characters ?? "").isEmpty ? event.charactersIgnoringModifiers ?? "" : event.characters ?? ""
- guard let scalar = text.unicodeScalars.first else { return false }
+ guard let scalar = text.unicodeScalars.first else { return true }
return !CharacterSet.controlCharacters.contains(scalar)
}
diff --git a/native/apple/Tests/SheetsKitTests/FirstKeystrokeTests.swift b/native/apple/Tests/SheetsKitTests/FirstKeystrokeTests.swift
index 56f3c61..8f3a1d7 100644
--- a/native/apple/Tests/SheetsKitTests/FirstKeystrokeTests.swift
+++ b/native/apple/Tests/SheetsKitTests/FirstKeystrokeTests.swift
@@ -1,5 +1,6 @@
#if os(macOS)
import AppKit
+import Carbon
import SwiftUI
import XCTest
import RastrilloApple
@@ -212,6 +213,46 @@ final class FirstKeystrokeTests: XCTestCase {
XCTAssertEqual(server.value(100, 0), "x")
}
+ /// Review #72, finding 4: a layout's own dead key (Spanish ´, German ^)
+ /// needs no Option and its key-down may carry no characters at all. The
+ /// layouts are read with UCKeyTranslate, not selected, so the machine's
+ /// keyboard is left alone: find each layout's unmodified dead keys, check
+ /// they compose, and check such a key-down starts typing whatever AppKit
+ /// puts in its character fields.
+ @MainActor func testLayoutDeadKeysWithoutOptionStartTyping() throws {
+ for id in ["com.apple.keylayout.Spanish-ISO", "com.apple.keylayout.German"] {
+ let filter = [kTISPropertyInputSourceID as String: id] as CFDictionary
+ guard let list = TISCreateInputSourceList(filter, true)?.takeRetainedValue() as? [TISInputSource],
+ let source = list.first,
+ let raw = TISGetInputSourceProperty(source, kTISPropertyUnicodeKeyLayoutData) else {
+ XCTFail("\(id) is not installed"); continue
+ }
+ let data = Unmanaged<CFData>.fromOpaque(raw).takeUnretainedValue() as Data
+ var deadKeys: [(UInt16, String)] = []
+ data.withUnsafeBytes { buffer in
+ let layout = buffer.bindMemory(to: UCKeyboardLayout.self).baseAddress!
+ func translate(_ code: UInt16, _ state: inout UInt32) -> String {
+ var chars = [UniChar](repeating: 0, count: 4); var length = 0
+ UCKeyTranslate(layout, code, UInt16(kUCKeyActionDown), 0, UInt32(LMGetKbdType()), 0, &state, 4, &length, &chars)
+ return String(utf16CodeUnits: chars, count: length)
+ }
+ for code in UInt16(0)..<UInt16(51) {
+ var state: UInt32 = 0
+ guard translate(code, &state).isEmpty, state != 0 else { continue }
+ deadKeys.append((code, translate(14, &state))) // then e
+ }
+ }
+ XCTAssertFalse(deadKeys.isEmpty, "\(id) has an unmodified dead key")
+ XCTAssertTrue(deadKeys.contains { ["é", "ê", "è"].contains($0.1) }, "\(id): a dead key then e composes (\(deadKeys))")
+ for (code, _) in deadKeys {
+ for ignoring in ["", "´", "^"] {
+ let event = NSEvent.keyEvent(with: .keyDown, location: .zero, modifierFlags: [], timestamp: 0, windowNumber: 0, context: nil, characters: "", charactersIgnoringModifiers: ignoring, isARepeat: false, keyCode: code)!
+ XCTAssertTrue(SheetTypeAhead.startsTyping(event), "\(id) dead key \(code) with ignoring \"\(ignoring)\"")
+ }
+ }
+ }
+ }
+
@MainActor func testWhichKeysStartTyping() {
func event(_ chars: String, _ ignoring: String, _ code: UInt16, _ flags: NSEvent.ModifierFlags = []) -> NSEvent {
NSEvent.keyEvent(with: .keyDown, location: .zero, modifierFlags: flags, timestamp: 0, windowNumber: 0, context: nil, characters: chars, charactersIgnoringModifiers: ignoring, isARepeat: false, keyCode: code)!