Continue tracking the mouse drag event even after moving the cursor out of the movie

I have multiple views aligned to grids in parent view (all are NSView)

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

To be specific, I'm drawing rectangular rectangles while dragging with the mouse in the child view.

Problem: When the cursor is moved out of the child view (while the mouse is being dragged), it is obvious that I cannot track the event and hence I cannot resize the rectangle. I want to track mouse movements even outside the application window ... (just a drag event for now)

Is there any obvious or difficult way to achieve this.

Thanks in Advance

Rajesh

+1


source to share


2 answers


- (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


source


I want to provide an alternative to the accepted answer that does not involve trapping events in a while loop.

Handle the mouse drag event:



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

      

This will work if you start dragging inside the NSView and keep burning if you drag the mouse outside of it.

+1


source







All Articles