macoscocoansviewnsbuttonnscontrol

NSButton mouseDown mouseUp behaving differently on enabled


If the NSButton is not enabled, the mouseDown: and mouseUp: behave as expected (so when the mouse is pushed down, the mouseDown: is called, and when it is released, the mouseUp: is called)

However, if the NSButton IS enabled, than the mouseUp: doesn't get called at all, and mouseDown: is called AFTER the mouse has been released

- (void)mouseDown:(NSEvent *)theEvent {
    [super mouseDown:theEvent];
}

- (void)mouseUp:(NSEvent *)theEvent {
    [super mouseUp:theEvent];
}

Why is this behaving differently, and how can i force the correct behaviour (the same as when the button is NOT enabled)


Solution

  • To cobble off of the previous if the desire is to create a custom button whose action method can distinguish down presses from releases, try adding the property isPressed to it, and the following code:

     (void)mouseDown:(NSEvent *)event
     {
        self.isPressed = true;  
        [super mouseDown:event];
    
        self.isPressed = false;
    
        [self.target performSelector:self.action withObject:self];
    }
    

    The custom button must be set to send actions per:

    [self.button sendActionOn: NSLeftMouseDownMask | NSLeftMouseUpMask];
    

    Without this, the action method is not called until the button is released.

    In the action method, isPressed can be queried. e.g:

    int state = (int)[sender isPressed];
    

    An annoying but harmless "Feature" of this is that the action method is called twice when the button is released: once from inside NSButton with isPressed still true. (This should be ignored.) The second time from custom button's performSelector method, with isPressed false.

    Any comments on if this is likely to work on future releases?