From d6a6d24054f90e2f2c077a6eef796d3d7038c35e Mon Sep 17 00:00:00 2001 From: Carsten Abele Date: Tue, 7 Apr 2026 19:40:17 +0200 Subject: [PATCH] feat: add CursorInjector with CGEvent keystroke simulation --- .../MyVoxtral/Utilities/CursorInjector.swift | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 MyVoxtral/MyVoxtral/Utilities/CursorInjector.swift diff --git a/MyVoxtral/MyVoxtral/Utilities/CursorInjector.swift b/MyVoxtral/MyVoxtral/Utilities/CursorInjector.swift new file mode 100644 index 0000000..070960b --- /dev/null +++ b/MyVoxtral/MyVoxtral/Utilities/CursorInjector.swift @@ -0,0 +1,29 @@ +import ApplicationServices + +struct CursorInjector { + static var isAccessibilityGranted: Bool { + AXIsProcessTrusted() + } + + static func promptAccessibilityPermission() { + let options = [kAXTrustedCheckOptionPrompt.takeUnretainedValue() as String: true] as CFDictionary + AXIsProcessTrustedWithOptions(options) + } + + static func typeText(_ text: String) { + guard isAccessibilityGranted else { return } + + let source = CGEventSource(stateID: .hidSystemState) + + for character in text { + let string = String(character) + let event = CGEvent(keyboardEventSource: source, virtualKey: 0, keyDown: true) + event?.keyboardSetUnicodeString(stringLength: string.utf16.count, unicodeString: Array(string.utf16)) + event?.post(tap: .cghidEventTap) + + let eventUp = CGEvent(keyboardEventSource: source, virtualKey: 0, keyDown: false) + eventUp?.keyboardSetUnicodeString(stringLength: string.utf16.count, unicodeString: Array(string.utf16)) + eventUp?.post(tap: .cghidEventTap) + } + } +}