CGEventPost doesn't work in 10.14

I can't create mouse or keyboard events in 10.14. Is this a bug or is Apple changing or removing this capability?

Replies

Could you show the code where you try.


And what error do you get ? Complie error ? Runtime ?


What error message in each case ?

I'm doing it like this:


CGEventType et = kCGEventMouseMoved;

CGMouseButton mb = kCGMouseButtonLeft;

int mouseEventTapLocation = kCGSessionEventTap;


CGEventRef ev = CGEventCreateMouseEvent(NULL, et , mousePosition, mb);

CGEventSetFlags(ev, 0);

CGEventSetType(ev, et);

CGEventPost(mouseEventTapLocation, ev);


It looks like all remote desktop software is currently broken with 10.14, so it's possible Apple removed virtual mouse events, broke them, or changed the way they work, but I haven't seen anything showing a new way to do this. I'm not getting any errors, it just doesn't work.


Thanks

That could be a security (sandboxing) issue, to prevent an external program to take control through the mouse and do unauthorized things. I remember reading something about this, but can't remember where.


have a look here, I found some interesting hints at the end.

h ttps://stackoverflow.com/questions/41908620/how-to-simulate-mouse-click-from-mac-app-to-other-application


EDITED. Finally found more relevant:

h ttps://stackoverflow.com/questions/47086375/how-to-post-a-quartz-event-after-swift-application-launch


and

h ttps://developer.apple.com/library/archive/documentation/Security/Conceptual/AppSandboxDesignGuide/DesigningYourSandbox/DesigningYourSandbox.html


"Posting keyboard or mouse events to another app

You cannot sandbox an app that controls another app. Posting keyboard or mouse events using functions like CGEventPost offers a way to circumvent this restriction, and is therefore not allowed from a sandboxed app."

We are changing the default system permissions for using CGEventTaps and CGEvent posting to remove this capability without explicit user permission.


I believe the intended behavior is that the first time that your application attempts to post an event, you should see an alert stating that application "Foo" is trying to control the computer using Accessibility feature, and suggesting that you can grant permission to do this in System Prefs, Security & Privacy pref pane. In that pref pane, go to the Privacy tab, choose Accessibility from the left list, and you should see your app show up, initially without a checkbox. If you enter an admin password and check the checkbox, your app should be able to post events again.

We've always been prompting for permission. Our app is added and checked under Accessibility, but it stopped working with 10.14. Is that the only place I would need to provide permission? I've tried several other remote desktop apps that also stopped working, even though they also have permission too.

Thanks!

Eric, in the past Quartz event taps required Security & Privacy authorization only for keyboard events. Are you saying that Mojave is extending this to all event taps (just like the accessibility API)?

Yes, that is correct.

Probably best to file a bug about that, with instructions to reproduce with your app. Maybe there's a case that we're missing.

Thanks, I posted a bug report for the case.

It seems that creating listenOnly event taps is now restricted in Mojave. Is this intentional? The WWDC talk seems to indicate that there were no changes when creating event taps that cannot modify events.

I'm the author of the mac app, Keytty (https://keytty.com) which also uses the same API and it doesn't work on Mohave even though it's allowed in Settings > Security > Accesibility. I will file the bug, too.

Can you post the radar link for this bug? We are experiencing it too and I'd like to follow the progress and figure out whether there's anything we can add by filing our own bug.

I had the same issue on Catalina, but removing the app from Accessibility settings and relaunching the app fixed the issue.