feat: add CursorInjector with CGEvent keystroke simulation

This commit is contained in:
Carsten Abele 2026-04-07 19:40:17 +02:00
parent 295cd5a500
commit d6a6d24054

View file

@ -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)
}
}
}