0

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

4

1 回答 1

1

是的,它是“一个运行循环的东西”——你正在“休眠”主线程,因此阻止它以通常的方式处理拖放内容。

我认为您可以通过将现有块更改为发送到dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)or来解决此问题..._PRIORITY_HIGH

于 2013-10-11T00:23:25.260 回答