continue tracking mouse-drag event even after cursor moves out of the movie

▼魔方 西西 提交于 2020-01-25 02:52:28

问题


I have few views aligned in grids in parent view (All being NSView's )

I am overriding -(void)mouseDown:(NSEvent *)event - (void)mouseDragged:(NSEvent *)theEvent for some custom drawing in child view subclass

To be specific, I draw some rectangle boxes during mouse drag in child view's.

Problem: when cursor moves out of the child view( during mouse drag ) , obviously, I am not able to track the event and hence I cannot resize the rectangle. I want to track the mouse movements even outside the application window... (for now just the drag event )

Is there any obvious or complex way to achieve this.....

Thanks in Advance

Rajesh


回答1:


- (void)mouseDown:(NSEvent *)theEvent
{
    NSPoint point;
    while (1) {
        theEvent = [[self window] nextEventMatchingMask: (NSLeftMouseDraggedMask | NSLeftMouseUpMask)];
        point = [self convertPoint: [theEvent locationInWindow] fromView: nil];

        // do something with point

        if ([theEvent type] == NSLeftMouseUp)
            break;
    }
}



回答2:


I want to present an alternative to the accepted answer which doesn't involve intercepting events in a while loop.

Handle the mouse dragged event:

- (void)mouseDragged:(NSEvent *)theEvent
{
}

This will fire if you start a drag inside an NSView and will continue to fire if you drag the mouse outside it.



来源:https://stackoverflow.com/questions/760717/continue-tracking-mouse-drag-event-even-after-cursor-moves-out-of-the-movie

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!