问题
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