NSButton 鼠标按下事件
我试图在鼠标按下按钮时显示弹出菜单。当鼠标按下时,按钮应显示为按下状态,而当鼠标松开时,按钮应显示为“未按下”,无论选择了任何菜单项。类似于“Expose/Space Preference”面板中用于添加应用程序的“+”按钮。
到目前为止,我尝试了 3 种方法:
单击按钮时发送操作。但这里弹出菜单出现在鼠标向上而不是鼠标向下时
子类 NSButton 并覆盖 mouseDown: 上。鼠标按下时会出现弹出菜单,我可以选择任何菜单项。但是当弹出菜单消失时,按钮似乎被按下了。因此,我需要再次单击“取消按下”按钮,然后才能再次执行相同的操作。
NSEvent addLocalMonitorForEventsMatchingMask。与 2 类似的行为。
有什么建议吗?我想方法2或3是正确的方法,只需“取消按下”鼠标上的按钮即可。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Swift“语法”
或者:
NSButton 子类:
Swift "Syntax"
Alternatively:
Subclass NSButton:
相当古老的线程。但这是我最近面临的问题,所以如果允许我做出贡献...
在我的情况下,使用 NSPopupButton 不是一个选择。因为 NSPopupButton 创建了它自己的 NSMenu,我不需要它(而且我不知道如何让它消失)。
我找到了另一种方法。可以更改默认的 NSButton 行为而不对其进行子类化。只需使用 NSControl 类的
-sendActionOn:
方法(在-awakeFromNib
中的某个位置):现在按钮的操作将在 mouseDown 事件上发送。
希望这有帮助。
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
):Now the action from the button is sent on mouseDown event.
Hope this helps.
正如 Peter 提到的,您可以使用 NSPopUpButton。不明显的是,您可以配置按钮的样式、图像、标题等,就像使用 NSButton 一样。
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.