Skip to content

Fastest and only macOS Dictation app with on-device STT and custom trained AI enhancement model. Windows, iOS testflight! Android, Linux waitlist open! A local Wispr Flow alternative. Follow https://x.com/fluidvoiceapp for more

fix: use layout-aware UCKeyTranslate fallback for unknown key codes

Paul Somers154e4a5692a8
diff --git a/Sources/Fluid/Models/HotkeyShortcut.swift b/Sources/Fluid/Models/HotkeyShortcut.swift
index 291df1aa..86856138 100644
--- a/Sources/Fluid/Models/HotkeyShortcut.swift
+++ b/Sources/Fluid/Models/HotkeyShortcut.swift
@@ -1,4 +1,5 @@
import AppKit
+import Carbon
import Foundation
struct HotkeyShortcut: Codable, Equatable {
@@ -15,8 +16,10 @@ struct HotkeyShortcut: Codable, Equatable {
if self.modifierFlags.contains(.shift) { parts.append("⇧") }
if let key = Self.keyCodeToString(keyCode) {
parts.append(key)
+ } else if let key = Self.characterForKeyCode(keyCode) {
+ parts.append(key)
} else {
- parts.append(String(Character(UnicodeScalar(self.keyCode) ?? "?")))
+ parts.append("?")
}
if self.modifierFlags.isEmpty {
@@ -97,6 +100,41 @@ struct HotkeyShortcut: Codable, Equatable {
default: return nil
}
}
+
+ /// Uses the current keyboard layout to resolve a key code to its displayed character.
+ private static func characterForKeyCode(_ keyCode: UInt16) -> String? {
+ guard let sourceRef = TISCopyCurrentKeyboardLayoutInputSource()?.takeRetainedValue(),
+ let rawPtr = TISGetInputSourceProperty(sourceRef, kTISPropertyUnicodeKeyLayoutData)
+ else { return nil }
+
+ let layoutData = Unmanaged<CFData>.fromOpaque(rawPtr).takeUnretainedValue() as Data
+ return layoutData.withUnsafeBytes { buffer -> String? in
+ guard let layoutPtr = buffer.baseAddress?.assumingMemoryBound(to: UCKeyboardLayout.self) else {
+ return nil
+ }
+ var deadKeyState: UInt32 = 0
+ var chars = [UniChar](repeating: 0, count: 4)
+ var length = 0
+ let status = UCKeyTranslate(
+ layoutPtr,
+ keyCode,
+ UInt16(kUCKeyActionDisplay),
+ 0,
+ UInt32(LMGetKbdType()),
+ UInt32(kUCKeyTranslateNoDeadKeysMask),
+ &deadKeyState,
+ chars.count,
+ &length,
+ &chars
+ )
+ guard status == noErr, length > 0 else { return nil }
+ let result = String(utf16CodeUnits: chars, count: length).uppercased()
+ guard !result.isEmpty, !result.unicodeScalars.contains(where: { $0.value < 0x20 }) else {
+ return nil
+ }
+ return result
+ }
+ }
}
extension HotkeyShortcut {