So basically I'm making an auto clicker for Mac in Xcode 16.0, but the CGEvent doesn't seem to work. I searched the internet and viewed documentation, but I couldn't find much about it. Here's my code
let source = CGEventSource(stateID: .hidSystemState)
let mouseLocation = NSEvent.mouseLocation
CGEvent.init(mouseEventSource: source, mouseType: .leftMouseDown, mouseCursorPosition: mouseLocation, mouseButton: .left)?.post(tap: .cghidEventTap)
usleep(100_000)
CGEvent.init(mouseEventSource: source, mouseType: .leftMouseUp, mouseCursorPosition: mouseLocation, mouseButton: .left)?.post(tap: .cghidEventTap)
I also tried moving the mouse but nothing. I also gave it access to control my computer (in settings > privacy & security > accessibility > my app) as it is required to do everything with mouse and keyboard. But still no.
Ran into this as well - seems like Sequoia has started validating the timestamp of the event, try constructing the mouse event via CGEvent.mouseEvent (since it does not seem that any of the CGEvent.init
signatures allow providing a timestamp) and provide a correct timestamp derived from the system uptime and also a proper event number (even thought seems that only setting a proper timestamp is fine as well). At least that is what solved this for me.