CGEvent to simulate paste command silently fails on High Sierra

I have the following piece of code that is used to trigger pasting of what is currently in the pasteboard:

Code Block swift
let vCode = UInt16(kVK_ANSI_V)
let source = CGEventSource(stateID: .combinedSessionState)
source?.setLocalEventsFilterDuringSuppressionState([.permitLocalMouseEvents, .permitSystemDefinedEvents],
state: .eventSuppressionStateSuppressionInterval)
let keyVDown = CGEvent(keyboardEventSource: source, virtualKey: vCode, keyDown: true)
let keyVUp = CGEvent(keyboardEventSource: source, virtualKey: vCode, keyDown: false)
keyVDown?.flags = .maskCommand
keyVUp?.flags = .maskCommand
keyVDown?.post(tap: .cgAnnotatedSessionEventTap)
keyVUp?.post(tap: .cgAnnotatedSessionEventTap)


The code worked fine and reliably until I enabled App Sandbox. Now, the code still works in Catalina and Mojave, but silently fails in High Sierra. The application has full accessibility permissions.

This code is used in my open source clipboard manager - Maccy. You can see full code in https://github.com/p0deje/Maccy/blob/16ded91a2779b67ebafec92188a61d0b7e713680/Maccy/Clipboard.swift#L87-L100.

CGEvent to simulate paste command silently fails on High Sierra
 
 
Q