CGEvent Not Working

I am trying to simulate a paste command and it seems to not want to paste. It worked at one point with the same code and now is causing issues.

My code looks like this: ` func simulatePaste() { guard let source = CGEventSource(stateID: .hidSystemState) else { print("Failed to create event source") return }

    let keyDown = CGEvent(keyboardEventSource: source, virtualKey: CGKeyCode(9), keyDown: true)
    let keyUp = CGEvent(keyboardEventSource: source, virtualKey: CGKeyCode(9), keyDown: false)

    keyDown?.flags = .maskCommand
    keyUp?.flags = .maskCommand

    keyDown?.post(tap: .cgAnnotatedSessionEventTap)
    keyUp?.post(tap: .cgAnnotatedSessionEventTap)

    print("Simulated Cmd + V")
}

I know that there is some issues around permissions and so in my Info.plist I have this:

        <string>NSApplication</string>
        <key>NSAppleEventsUsageDescription</key>
        <string>This app requires permission to send keyboard input for pasting from the clipboard.</string>

I have also disabled sandbox. It does ask me if I want to give the app permissions but after approving it, it still doesn't paste.

Written by rohitsaxena in 773989021
I have also disabled sandbox.

Do you plan to ship this product on the Mac App Store?

That matters because the Mac App Store requires the App Sandbox, and that has a significant impact on this issue. If you plan to distribute via the Mac App Store then you should leave the sandbox enabled while testing this stuff. If not, it’s easier to first disable the sandbox and then, once you have everything working, choose whether to re-enable it.

Written by rohitsaxena in 773989021
in my Info.plist I have this:

That’s irrelevant to this issue. You’re dealing with low-level events and the NSAppleEventsUsageDescription is about the Apple events.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

CGEvent Not Working
 
 
Q