problem occurs when creating it in MacOS App with SandBox
This is working for me. I have a test project I use for exploring various TCC options. It can build either with or without the sandbox. I ran the sandboxed version on my Mac (macOS 13.2.1) and started the event tap test. I saw this alert:
Once I approved this in System Settings my event tap started to work.
The specific code I’m using is pasted in below.
If you can’t get things working based on this, I recommend that you open a DTS tech support incident and I can look at your issue in more depth.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"
os_log(.debug, log: self.log, "will create tap")
let info = Unmanaged.passRetained(self).toOpaque()
let mask = CGEventMask(1 << CGEventType.keyDown.rawValue)
guard let port = CGEvent.tapCreate(
tap: .cgSessionEventTap,
place: .headInsertEventTap,
options: .listenOnly,
eventsOfInterest: mask,
callback: { (proxy, type, event, info) -> Unmanaged<CGEvent>? in
let obj = Unmanaged<CGEventTapAction>.fromOpaque(info!).takeUnretainedValue()
obj.didReceiveEvent(event)
// We don’t replace the event, so the new event is the same as
// the old event, so we return it unretained.
return Unmanaged.passUnretained(event)
},
userInfo: info
) else {
os_log(.debug, log: self.log, "did not create tap")
// We retained `self` above, but the event tap didn’t get created so
// we need to clean up.
Unmanaged<CGEventTapAction>.fromOpaque(info).release()
setStatus("Failed to create event tap.")
return
}
let rls = CFMachPortCreateRunLoopSource(nil, port, 0)!
CFRunLoopAddSource(CFRunLoopGetCurrent(), rls, .defaultMode)
self.runState = RunState(port: port, setStatus: setStatus)
os_log(.debug, log: self.log, "did create tap")