feat: add CursorInjector with CGEvent keystroke simulation
This commit is contained in:
parent
295cd5a500
commit
d6a6d24054
1 changed files with 29 additions and 0 deletions
29
MyVoxtral/MyVoxtral/Utilities/CursorInjector.swift
Normal file
29
MyVoxtral/MyVoxtral/Utilities/CursorInjector.swift
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue