Hello everyone,
I’m currently working on a project where I need to simulate mouse drag and drop events in MacOS using Swift. I have written the following code which works well in most applications, but I’m encountering issues in some applications like Finder where the leftMouseDragged event doesn’t seem to work correctly. Click events are working fine everywhere.
Here’s the code I’m using for the drag event:
let location = CGEventTapLocation.cghidEventTap
let source = CGEventSource.init(stateID: .combinedSessionState)
let clickAtStart = CGEvent(mouseEventSource: source, mouseType: .leftMouseDown, mouseCursorPosition: locationPoint , mouseButton: .left)
let dragAndDrop = CGEvent(mouseEventSource: source, mouseType: .leftMouseDragged, mouseCursorPosition: locationPointDrag, mouseButton: .left)
clickAtStart?.post(tap: location)
usleep(200000)
dragAndDrop?.post(tap: location)
And analogic for drop event:
let location = CGEventTapLocation.cghidEventTap
let source = CGEventSource.init(stateID: .hidSystemState)
let dragAndDropRelease = CGEvent(mouseEventSource: source, mouseType: .leftMouseUp, mouseCursorPosition: locationPoint, mouseButton: .left)
dragAndDropRelease?.post(tap: location)
I’ve tried changing the order of events, using different event types, and checking the permissions of my app, but none of these solutions have worked. I’m wondering if anyone has encountered a similar issue or has any suggestions on how to resolve this.
Any help would be greatly appreciated!