Reputation: 1729
I'm trying to show a pop up menu on mouse down on a button. The button should appear pressed while mouse down, and be "un pressed" on mouse up regardless of any of the menu item been selected. Similar to the Expose/Space Preference panel "+" button for adding application.
So far I tried 3 methods:
Sent action when button is click. But here the pop up menu appear on mouse up instead of mouse down
Subclass NSButton and overwrite mouseDown:. The pop up menu appear on mouse down, I can select any of the menu item. But when the pop up menu is gone, the button appeared pressed. Hence I need to click once again to "un press" the button before I can get the same action again.
NSEvent addLocalMonitorForEventsMatchingMask. Similar behavior to 2.
Any suggestion? I guess method 2 or 3 is the right way, just need to "un press" the button on mouse up.
Upvotes: 4
Views: 4792
Reputation: 1442
Quite an old thread. But it's the problem I faced recently, so if I am allowed to contribute...
In my case using NSPopupButton wasn't an option. Because NSPopupButton creates it's own NSMenu that I don't need (and I don't know how to make it go away).
And I found another way. There is a possibility to change the default NSButton behaviour without subclassing it. Just use -sendActionOn:
method of NSControl class (somewhere in -awakeFromNib
):
[_myButton sendActionOn:NSEventMaskLeftMouseDown];
Now the action from the button is sent on mouseDown event. Hope this helps.
Upvotes: 14
Reputation: 41
Swift "Syntax"
myButton.sendAction(on: .leftMouseDown)
Alternatively:
Subclass NSButton:
class ButtonDown: NSButton {
override func awakeFromNib() {
super.awakeFromNib()
self.sendAction(on: .leftMouseDown)
}
}
Upvotes: 4
Reputation: 61228
As Peter mentioned, you can use an NSPopUpButton. What's not obvious is that you can configure the button's style, image, title, etc. just as you can with an NSButton.
Upvotes: 4