Skip to content

Open source CapCut alternative.

TypeScript

Use paste-backed keyboard text entry

David Le730c993117e8
diff --git a/apps/Donkey/Sources/DonkeyRuntime/MacKeyboardActionEngineInputBackend.swift b/apps/Donkey/Sources/DonkeyRuntime/MacKeyboardActionEngineInputBackend.swift
index c074000d..a21097a6 100644
--- a/apps/Donkey/Sources/DonkeyRuntime/MacKeyboardActionEngineInputBackend.swift
+++ b/apps/Donkey/Sources/DonkeyRuntime/MacKeyboardActionEngineInputBackend.swift
@@ -3,8 +3,44 @@ import CoreGraphics
import DonkeyContracts
import Foundation
+struct MacKeyboardTextEntryExecution: Equatable, Sendable {
+ var executed: Bool
+ var inputMode: String
+ var metadata: [String: String]
+
+ init(
+ executed: Bool,
+ inputMode: String,
+ metadata: [String: String] = [:]
+ ) {
+ self.executed = executed
+ self.inputMode = inputMode
+ self.metadata = metadata
+ }
+}
+
public struct MacKeyboardActionEngineInputBackend: ActionEngineInputBackend {
- public init() {}
+ private let keyCommandPoster: @Sendable (String) -> Bool
+ private let textEntryExecutor: @Sendable (
+ String,
+ @escaping @Sendable (String) -> Bool
+ ) async -> MacKeyboardTextEntryExecution
+
+ public init() {
+ self.keyCommandPoster = Self.postKeyCommand
+ self.textEntryExecutor = Self.pasteText
+ }
+
+ init(
+ keyCommandPoster: @escaping @Sendable (String) -> Bool,
+ textEntryExecutor: @escaping @Sendable (
+ String,
+ @escaping @Sendable (String) -> Bool
+ ) async -> MacKeyboardTextEntryExecution
+ ) {
+ self.keyCommandPoster = keyCommandPoster
+ self.textEntryExecutor = textEntryExecutor
+ }
public func execute(_ command: ActionEngineCommand) async -> ActionEngineInputBackendResult {
guard command.kind == .key, let key = command.key else {
@@ -22,11 +58,14 @@ public struct MacKeyboardActionEngineInputBackend: ActionEngineInputBackend {
let executed: Bool
let inputMode: String
+ var inputMetadata: [String: String] = [:]
if command.metadata["inputRole"] == "textEntry" {
- executed = postText(key)
- inputMode = "unicodeText"
+ let textResult = await textEntryExecutor(key, keyCommandPoster)
+ executed = textResult.executed
+ inputMode = textResult.inputMode
+ inputMetadata = textResult.metadata
} else {
- executed = postKeyCommand(key)
+ executed = keyCommandPoster(key)
inputMode = "keyCommand"
}
@@ -42,7 +81,7 @@ public struct MacKeyboardActionEngineInputBackend: ActionEngineInputBackend {
"inputMode": inputMode,
"elementClick": "false",
"key": key
- ]
+ ].merging(inputMetadata) { current, _ in current }
)
}
@@ -58,26 +97,50 @@ public struct MacKeyboardActionEngineInputBackend: ActionEngineInputBackend {
)
}
- private func postText(_ text: String) -> Bool {
- guard !text.isEmpty,
- let source = CGEventSource(stateID: .hidSystemState),
- let event = CGEvent(keyboardEventSource: source, virtualKey: 0, keyDown: true)
- else {
- return false
+ private static func pasteText(
+ _ text: String,
+ postKeyCommand: @escaping @Sendable (String) -> Bool
+ ) async -> MacKeyboardTextEntryExecution {
+ guard !text.isEmpty else {
+ return MacKeyboardTextEntryExecution(
+ executed: false,
+ inputMode: "pasteboardText",
+ metadata: ["reason": "emptyText"]
+ )
}
- let utf16 = Array(text.utf16)
- utf16.withUnsafeBufferPointer { buffer in
- event.keyboardSetUnicodeString(
- stringLength: utf16.count,
- unicodeString: buffer.baseAddress
+ let pasteboard = NSPasteboard.general
+ let snapshot = PasteboardSnapshot.capture(from: pasteboard)
+ pasteboard.clearContents()
+ guard pasteboard.setString(text, forType: .string) else {
+ snapshot.restore(to: pasteboard)
+ return MacKeyboardTextEntryExecution(
+ executed: false,
+ inputMode: "pasteboardText",
+ metadata: ["reason": "pasteboardWriteFailed"]
)
}
- event.post(tap: .cghidEventTap)
- return true
+ let textChangeCount = pasteboard.changeCount
+
+ try? await Task.sleep(nanoseconds: 30_000_000)
+ let pasted = postKeyCommand("Command+V")
+ try? await Task.sleep(nanoseconds: 400_000_000)
+ if pasteboard.changeCount == textChangeCount {
+ snapshot.restore(to: pasteboard)
+ }
+
+ return MacKeyboardTextEntryExecution(
+ executed: pasted,
+ inputMode: "pasteboardText",
+ metadata: [
+ "textEntry.method": "pasteboard",
+ "textEntry.shortcut": "Command+V",
+ "textEntry.characterCount": String(text.count)
+ ]
+ )
}
- private func postKeyCommand(_ command: String) -> Bool {
+ private static func postKeyCommand(_ command: String) -> Bool {
let parts = command
.split(separator: "+")
.map { String($0).trimmingCharacters(in: .whitespacesAndNewlines) }
@@ -196,3 +259,41 @@ public struct MacKeyboardActionEngineInputBackend: ActionEngineInputBackend {
)
}
}
+
+private struct PasteboardSnapshot {
+ var items: [Item]
+
+ struct Item {
+ var representations: [(type: String, data: Data)]
+ }
+
+ static func capture(from pasteboard: NSPasteboard) -> PasteboardSnapshot {
+ PasteboardSnapshot(
+ items: (pasteboard.pasteboardItems ?? []).map { item in
+ Item(
+ representations: item.types.compactMap { type in
+ guard let data = item.data(forType: type) else { return nil }
+ return (type.rawValue, data)
+ }
+ )
+ }
+ )
+ }
+
+ func restore(to pasteboard: NSPasteboard) {
+ pasteboard.clearContents()
+ let pasteboardItems = items.compactMap { item -> NSPasteboardItem? in
+ let pasteboardItem = NSPasteboardItem()
+ var wroteRepresentation = false
+ for representation in item.representations {
+ let type = NSPasteboard.PasteboardType(representation.type)
+ wroteRepresentation = pasteboardItem.setData(representation.data, forType: type)
+ || wroteRepresentation
+ }
+ return wroteRepresentation ? pasteboardItem : nil
+ }
+ if !pasteboardItems.isEmpty {
+ pasteboard.writeObjects(pasteboardItems)
+ }
+ }
+}
diff --git a/apps/Donkey/Tests/DonkeyRuntimeTests/MacKeyboardActionEngineInputBackendTests.swift b/apps/Donkey/Tests/DonkeyRuntimeTests/MacKeyboardActionEngineInputBackendTests.swift
new file mode 100644
index 00000000..54b4d177
--- /dev/null
+++ b/apps/Donkey/Tests/DonkeyRuntimeTests/MacKeyboardActionEngineInputBackendTests.swift
@@ -0,0 +1,103 @@
+@testable import DonkeyRuntime
+import DonkeyContracts
+import Foundation
+import Testing
+
+@Suite
+struct MacKeyboardActionEngineInputBackendTests {
+ @Test
+ func textEntryUsesPasteboardBackedPasteCommand() async {
+ let recorder = KeyboardCommandRecorder()
+ let backend = MacKeyboardActionEngineInputBackend(
+ keyCommandPoster: { command in
+ recorder.append(command)
+ return true
+ },
+ textEntryExecutor: { text, postKeyCommand in
+ let pasted = postKeyCommand("Command+V")
+ return MacKeyboardTextEntryExecution(
+ executed: pasted,
+ inputMode: "pasteboardText",
+ metadata: [
+ "textEntry.method": "pasteboard",
+ "textEntry.shortcut": "Command+V",
+ "textEntry.characterCount": String(text.count)
+ ]
+ )
+ }
+ )
+
+ let result = await backend.execute(command(
+ key: "Coldplay",
+ metadata: ["inputRole": "textEntry"]
+ ))
+
+ #expect(result.executed == true)
+ #expect(result.metadata["inputMode"] == "pasteboardText")
+ #expect(result.metadata["textEntry.method"] == "pasteboard")
+ #expect(result.metadata["textEntry.shortcut"] == "Command+V")
+ #expect(result.metadata["textEntry.characterCount"] == "8")
+ #expect(recorder.commands() == ["Command+V"])
+ }
+
+ @Test
+ func ordinaryKeyCommandsDoNotUseTextEntryPastePath() async {
+ let recorder = KeyboardCommandRecorder()
+ let backend = MacKeyboardActionEngineInputBackend(
+ keyCommandPoster: { command in
+ recorder.append(command)
+ return true
+ },
+ textEntryExecutor: { _, _ in
+ MacKeyboardTextEntryExecution(
+ executed: false,
+ inputMode: "pasteboardText",
+ metadata: ["reason": "unexpectedTextEntry"]
+ )
+ }
+ )
+
+ let result = await backend.execute(command(key: "Command+F"))
+
+ #expect(result.executed == true)
+ #expect(result.metadata["inputMode"] == "keyCommand")
+ #expect(result.metadata["reason"] == nil)
+ #expect(recorder.commands() == ["Command+F"])
+ }
+
+ private func command(
+ key: String,
+ metadata: [String: String] = [:]
+ ) -> ActionEngineCommand {
+ ActionEngineCommand(
+ id: "keyboard-\(key)",
+ traceID: "trace-keyboard",
+ targetID: "local-app-task-test",
+ kind: .key,
+ issuedAt: RunTraceTimestamp(
+ wallClock: Date(timeIntervalSince1970: 0),
+ monotonicUptimeNanoseconds: 0
+ ),
+ key: key,
+ metadata: metadata
+ )
+ }
+}
+
+private final class KeyboardCommandRecorder: @unchecked Sendable {
+ private let lock = NSLock()
+ private var recordedCommands: [String] = []
+
+ func append(_ command: String) {
+ lock.lock()
+ recordedCommands.append(command)
+ lock.unlock()
+ }
+
+ func commands() -> [String] {
+ lock.lock()
+ let commands = recordedCommands
+ lock.unlock()
+ return commands
+ }
+}