Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 856
Peter Lapisu Avatar asked Mar 13 '14 20:03

Peter Lapisu


People also ask

What is MouseDown and MouseUp?

MouseDown occurs when the user presses the mouse button; MouseUp occurs when the user releases the mouse button.

Is MouseDown the same as click?

Note: This differs from the click event in that click is fired after a full click action occurs; that is, the mouse button is pressed and released while the pointer remains inside the same element. mousedown is fired the moment the button is initially pressed.


1 Answers

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?

like image 102
Allen King Avatar answered Sep 18 '22 09:09

Allen King