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)
MouseDown occurs when the user presses the mouse button; MouseUp occurs when the user releases the mouse button.
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.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With