Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mouseEntered event disabled when mouseDown (NSEvents Mac)

I have created an NSButton class, and when rolling over my buttons it happily detects mouseEntered and mouseExited events. But as soon as the mouseDown event occurs, so long as the mouse it down, mouseEntered events are no longer called until the mouse button is lifted.

So, when the mouseDown event is called, mouseEntered or MouseExited events are no longer called, nor is mouseDown called when rolling over other buttons until I let go of the initial mouseDown.

So I would like to detect when my mouse enters while the mouse is down as well.

like image 619
Josh Kahane Avatar asked Dec 21 '22 11:12

Josh Kahane


2 Answers

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

like image 196
Josh Kahane Avatar answered Jan 08 '23 06:01

Josh Kahane


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];
    }
}
like image 39
jscs Avatar answered Jan 08 '23 04:01

jscs