I am trying to simulate moving the mouse, clicking on an item and dragging it.
The item in question is dragged via the pasteboard (If you were to do it manually).
The code works fine, apart from, the dragged item's drag image only appears at the end of the sequence, it does not display whilst the mouse is dragging,
I'm wondering if it's a runloop thing, or something I'm missing, any help appreciated!
//Underlying code of the draggable item (how it's dragged)
[self dragImage:_dragImage
at:dragPosition
offset:NSZeroSize
event:theEvent
pasteboard:pb
source:self
slideBack:YES];
and the code I am using to simulate, is this:
double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
for (int i = 0; i < 100; i++)
{
CGEventRef move1 = CGEventCreateMouseEvent(
NULL, kCGEventMouseMoved,
CGPointMake(100, 200+i),
kCGMouseButtonLeft
);
CGEventPost(kCGHIDEventTap, move1);
CFRelease(move1);
[NSThread sleepForTimeInterval:0.01];
}
CGEventRef click1_down = CGEventCreateMouseEvent(
NULL, kCGEventLeftMouseDown,
CGPointMake(100, 290),
kCGMouseButtonLeft
);
CGEventPost(kCGHIDEventTap, click1_down);
CFRelease(click1_down);
[NSThread sleepForTimeInterval:0.01];
//int i = 0;
for (int i = 0; i < 100; i++)
{
CGEventRef click1_drag = CGEventCreateMouseEvent(
NULL, kCGEventLeftMouseDragged,
CGPointMake(100+i, 290),
kCGMouseButtonLeft
);
CGEventPost(kCGHIDEventTap, click1_drag);
CFRelease(click1_drag);
[NSThread sleepForTimeInterval:0.01];
}
});
Useful link: Simulating mouse input programmatically in OS X