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(paste): release Command after the synthesized Cmd+V
altic-devc0118882ab69
diff --git a/Sources/Fluid/Services/PasteDeliveryCoordinator.swift b/Sources/Fluid/Services/PasteDeliveryCoordinator.swiftindex 28c51c8f..dffb80e3 100644--- a/Sources/Fluid/Services/PasteDeliveryCoordinator.swift+++ b/Sources/Fluid/Services/PasteDeliveryCoordinator.swift@@ -551,33 +551,41 @@ final class SystemPasteboardManager: PasteboardManaging {@MainActorfinal class SystemPasteCommandPoster: PasteCommandPosting {func postGlobalPasteCommand() async -> Bool {- guard AXIsProcessTrusted() else { return false }+ guard AXIsProcessTrusted(), let events = Self.makePasteEvents() else { return false }+ // The coordinator verifies pasteboard ownership before reaching this point,+ // so the temporary item is ready to consume without an additional delay.+ for event in events {+ event.post(tap: .cghidEventTap)+ }+ return true+ }++ /// Command down, V down, V up, Command up. Pressing and releasing Command+ /// explicitly matters: setting the flag on V alone leaves the HID state+ /// reporting Command as held, which later blocks the send key.+ nonisolated static func makePasteEvents() -> [CGEvent]? {let pasteKeyCode = TypingService.pasteVirtualKeyCode+ let commandKeyCode = CGKeyCode(kVK_Command)let source = CGEventSource(stateID: .combinedSessionState)- guard let vDown = CGEvent(- keyboardEventSource: source,- virtualKey: pasteKeyCode,- keyDown: true- ), let vUp = CGEvent(- keyboardEventSource: source,- virtualKey: pasteKeyCode,- keyDown: false- ) else {- return false+ guard let cmdDown = CGEvent(keyboardEventSource: source, virtualKey: commandKeyCode, keyDown: true),+ let vDown = CGEvent(keyboardEventSource: source, virtualKey: pasteKeyCode, keyDown: true),+ let vUp = CGEvent(keyboardEventSource: source, virtualKey: pasteKeyCode, keyDown: false),+ let cmdUp = CGEvent(keyboardEventSource: source, virtualKey: commandKeyCode, keyDown: false)+ else {+ return nil}+ cmdDown.flags = .maskCommandvDown.flags = .maskCommandvUp.flags = .maskCommand+ cmdUp.flags = []// Mark the paste as ours so the hotkey tap passes it through untouched.- vDown.setIntegerValueField(.eventSourceUserData, value: TypingService.synthesizedEventUserData)- vUp.setIntegerValueField(.eventSourceUserData, value: TypingService.synthesizedEventUserData)-- // The coordinator verifies pasteboard ownership before reaching this point,- // so the temporary item is ready to consume without an additional delay.- vDown.post(tap: .cghidEventTap)- vUp.post(tap: .cghidEventTap)- return true+ let events = [cmdDown, vDown, vUp, cmdUp]+ for event in events {+ event.setIntegerValueField(.eventSourceUserData, value: TypingService.synthesizedEventUserData)+ }+ return events}}diff --git a/Sources/Fluid/Services/TypingService.swift b/Sources/Fluid/Services/TypingService.swiftindex b5ad1d88..1bd569fd 100644--- a/Sources/Fluid/Services/TypingService.swift+++ b/Sources/Fluid/Services/TypingService.swift@@ -816,15 +816,35 @@ final class TypingService {return false}+ private static let physicalModifierKeys: [(name: String, code: CGKeyCode)] = [+ ("cmd", CGKeyCode(kVK_Command)), ("rcmd", CGKeyCode(kVK_RightCommand)),+ ("shift", CGKeyCode(kVK_Shift)), ("rshift", CGKeyCode(kVK_RightShift)),+ ("opt", CGKeyCode(kVK_Option)), ("ropt", CGKeyCode(kVK_RightOption)),+ ("ctrl", CGKeyCode(kVK_Control)), ("rctrl", CGKeyCode(kVK_RightControl)),+ ("fn", CGKeyCode(kVK_Function)),+ ]++ private static func heldPhysicalModifiers() -> [String] {+ self.physicalModifierKeys+ .filter { CGEventSource.keyState(.hidSystemState, key: $0.code) }+ .map(\.name)+ }++ /// Reads the modifier keys themselves. The session flag state is not+ /// reliable here: the synthesized Cmd+V paste can leave Command reported+ /// as held until the next real event, which used to time this wait out.private func waitForPhysicalModifiersToRelease(timeout: TimeInterval) async -> Bool {- let relevant: CGEventFlags = [.maskCommand, .maskControl, .maskAlternate, .maskShift, .maskSecondaryFn]let startedAt = ProcessInfo.processInfo.systemUptimewhile ProcessInfo.processInfo.systemUptime - startedAt < timeout {- if CGEventSource.flagsState(.combinedSessionState).isDisjoint(with: relevant) {+ if Self.heldPhysicalModifiers().isEmpty {return true}try? await Task.sleep(nanoseconds: 15_000_000)}+ DebugLogger.shared.warning(+ "Post-insertion send suppressed: modifiers still held after \(Int(timeout * 1000))ms held=\(Self.heldPhysicalModifiers()) sessionFlags=\(CGEventSource.flagsState(.combinedSessionState).rawValue)",+ source: "TypingService"+ )return false}diff --git a/Tests/FluidDictationIntegrationTests/SpokenSendTests.swift b/Tests/FluidDictationIntegrationTests/SpokenSendTests.swiftindex b426afa5..82796e80 100644--- a/Tests/FluidDictationIntegrationTests/SpokenSendTests.swift+++ b/Tests/FluidDictationIntegrationTests/SpokenSendTests.swift@@ -1,3 +1,4 @@+import Carbon.HIToolboximport CoreGraphics@testable import FluidVoice_Debugimport XCTest@@ -336,6 +337,30 @@ final class SpokenSendTests: XCTestCase {)}+ // MARK: - Adversarial: paste key sequence++ func testPasteSequencePressesAndReleasesCommandExplicitly() throws {+ let events = try XCTUnwrap(SystemPasteCommandPoster.makePasteEvents())+ XCTAssertEqual(events.count, 4)+ let keyCodes = events.map { CGKeyCode($0.getIntegerValueField(.keyboardEventKeycode)) }+ XCTAssertEqual(keyCodes, [+ CGKeyCode(kVK_Command), TypingService.pasteVirtualKeyCode, TypingService.pasteVirtualKeyCode, CGKeyCode(kVK_Command),+ ])+ // Modifier presses are flagsChanged events, exactly like a physical Command key.+ XCTAssertEqual(events.map(\.type), [.flagsChanged, .keyDown, .keyUp, .flagsChanged])+ XCTAssertTrue(events[0].flags.contains(.maskCommand))+ XCTAssertTrue(events[1].flags.contains(.maskCommand))+ XCTAssertTrue(events[2].flags.contains(.maskCommand))+ XCTAssertFalse(events[3].flags.contains(.maskCommand), "the final event must release Command")+ }++ func testPasteSequenceIsInvisibleToTheHotkeyTap() throws {+ let events = try XCTUnwrap(SystemPasteCommandPoster.makePasteEvents())+ for event in events {+ XCTAssertTrue(GlobalHotkeyManager.isSynthesizedTypingEvent(event))+ }+ }+func testImmediateStopCompletionRequiresTerminalPhraseAndSilence() {let arguments = (text: "Ready, send it.",