mouseEntered Event Disabled When mouseDown (NSEvents Mac)

两盒软妹~` 提交于 2019-12-06 03:44:16

Turns out I just needed to add NSTrackingEnabledDuringMouseDrag to my NSTrackingAreaOptions. mouseEntered and mouseExited events now fire when dragging with mouse down.

When an NSButton receives a mouse down event, it enters a private tracking loop, handling all the mouse events that are posted until it gets a mouse up. You can set up your own tracking loop to do things based on the mouse location:

- (void) mouseDown:(NSEvent *)event {

    BOOL keepTracking = YES;
    NSEvent * nextEvent = event;

    while( keepTracking ){

        NSPoint mouseLocation = [self convertPoint:[nextEvent locationInWindow]
                                          fromView:nil];
        BOOL mouseInside = [self mouse:mouseLocation inRect:[self bounds]];
        // Draw highlight conditional upon mouse being in bounds
        [self highlight:mouseInside];

        switch( [nextEvent type] ){
            case NSLeftMouseDragged:
                /* Do something interesting, testing mouseInside */
                break;
            case NSLeftMouseUp:
                if( mouseInside ) [self performClick:nil];
                keepTracking = NO;
                break;
            default:
                break;
        }

        nextEvent = [[self window] nextEventMatchingMask:NSLeftMouseDraggedMask | NSLeftMouseUpMask];
    }
}

When the left mouse button is down, dragging begins. If I remember correctly, mouse moved events aren't sent during drags, and that's probably one reason that you don't get mouseEntered and mouseExited messages. However, if you implement the NSDraggingDestination protocol and register your view as a possible recipient of the type of data being dragged, you'll get draggingEntered and draggingExited messages instead.

Read about it in the Dragging Destinations section of Drag and Drop Programming Topics.

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