10.5 中的 NSMenu 按下鼠标按钮
我正在更新(降级?)我为 10.6+ 编写的应用程序,以便在 10.5+ 中运行。我正在努力捕获 -(void)menuWillOpen:(NSMenu *);
选择器中当前按下的鼠标按钮。
对于 10.6+,我正在利用 [NSEvent PressMouseButtons]
它允许我在事件流之外获取按下的按钮。但是,这在 10.5+ 中不存在(看来我需要调用 [theEvent buttonNumber]
。
如何捕获按下的鼠标按钮(右或左):
- 在我的 NSMenu 委托内部
- 最好在内部
-(void)menuWillOpen:(NSMenu *)menu
选择器 - 在一个同时适用于 10.5+ 和 10.6+ 版本的庄园中,
我非常感谢您的帮助,并且知道 StackOverflow 将帮助新的 Objective-C 程序员!
谢谢, 达斯汀
I’m updating (downdating?) an application I’ve written for 10.6+ to work in 10.5+. I’m struggling with capturing the currently pressed mouse button in the -(void)menuWillOpen:(NSMenu *);
selector.
For 10.6+ I’m taking advantage of the [NSEvent pressedMouseButtons]
which allows me to get the pressed button outside the event stream. However, this doesn’t exist in 10.5+ (it appears I need to call [theEvent buttonNumber]
.
How do I capture the pressed mouse button (right or left):
- Inside of my NSMenu delegate
- Preferably inside the
-(void)menuWillOpen:(NSMenu *)menu
selector - In a manor that works in both 10.5+ and 10.6+
I really appreciate the help and know StackOverflow will help a new Objective-C programmer out!
Thanks,
Dustin
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最后,我能够通过调用获取当前的鼠标按钮(感谢 Nick Paulson 的帮助):
[[[NSApplicationsharedApplication]currentEvent]buttonNumber]
正如ughoavgfhw指出了一种获取相同内容的更短方法事件是:
[[NSApp currentEvent] buttonNumber]
In the end I was able get the current mouse button by calling (thanks to Nick Paulson for the help):
[[[NSApplication sharedApplication] currentEvent] buttonNumber]
As ughoavgfhw pointed out a shorter way of fetching the same event is though:
[[NSApp currentEvent] buttonNumber]
另一种方法是创建一个捕获鼠标按下事件的 CGEventTap。
然后,您只需记住最后一个事件类型并在您的 NSMenu 委托方法之一中进行检查。
下面是一些示例代码:
Quartz 事件服务文档指出:
我仔细检查了一下,看来您确实不必启用辅助设备来接收鼠标事件。
但如果
[[[NSApplication sharedApplication] currentEvent] buttonNumber]
满足您的要求,我肯定会同意。它的级别更高,因此代码更少。Another way would be to create a CGEventTap that captures mouse down events.
You then just have to remember the last event type and check that in one of your NSMenu delegate methods.
Here is some sample code:
The Quartz Event Services docs state that:
I double checked that and it seems that you really don't have to enable assistive devices to receive mouse events.
But if
[[[NSApplication sharedApplication] currentEvent] buttonNumber]
does what you want, I'd definitely go with that. It's higher level and therefore less code.