Cocoa 中是否有用于 Windows 中同步 TrackPopupMenu 的等效技术?
为了响应 rightMouse 事件,我想调用一个显示上下文菜单、运行它并响应所选菜单项的函数。 在 Windows 中,我可以使用带有 TPM_RETURNCMD 标志的 TrackPopupMenu。
在 Cocoa 中实现这个最简单的方法是什么? 看来 NSMenu:popUpContextMenu 想要将事件发布到指定的 NSView。 我必须创建一个虚拟视图并在返回之前等待事件吗? 如果是这样,鉴于我没有返回到我的 main ,我该如何“等待”或刷新事件?
In response to a rightMouse event I want to call a function that displays a context menu, runs it, and responds to the selected menu item. In Windows I can use TrackPopupMenu with the TPM_RETURNCMD flag.
What is the easiest way to implement this in Cocoa? It seems NSMenu:popUpContextMenu wants to post an event to the specified NSView. Must I create a dummy view and wait for the event before returning? If so, how do I "wait" or flush events given I am not returning to my main ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 Cocoa 中执行此操作的“正确”方法是让菜单项的目标和操作执行所需的方法。 但是,如果您必须在初始调用中执行此操作,则可以使用
[NSView nextEventMatchingMask:]
不断获取您感兴趣的新事件、处理它们并循环。 这是一个只等待鼠标右键被释放的示例。 您可能想要使用更复杂的掩码参数,并不断调用[NSView nextEventMatchingMask:]
直到获得您想要的结果。<代码>
我想你会找到“正确”的方式来变得更容易。
The 'proper' way to do this in Cocoa is to have your menu item's target and action perform the required method. However, if you must do it within your initial call, you can use
[NSView nextEventMatchingMask:]
to continually fetch new events that interest you, handle them, and loop. Here's an example which just waits until the right mouse button is released. You'll probably want to use a more complex mask argument, and continually call[NSView nextEventMatchingMask:]
until you get what you want.I think you'll find the 'proper' way to go much easier.
看来 popUpContextMenu 已经是同步的。 由于我没有找到一种在不向 NSView 发送通知的情况下使用 NSMenu 的方法,所以我想出了一个实例化临时 NSView 的方案。 目标是显示弹出菜单并在单个函数调用的上下文中返回所选项目。 以下是我提出的解决方案的代码片段:
It appears that popUpContextMenu is already synchronous. Since I didn't see a way to use NSMenu without having it send a notification to an NSView I came up with a scheme that instantiates a temporary NSView. The goal is to display a popup menu and return the selected item in the context of a single function call. Following is code snippets of my proposed solution:
除了已被弃用的 Carbon 之外,没有直接的等价物。
要检测右键单击,请按照这些说明进行操作。 它们确保您能够正确检测右键单击和右键按住,并在应该显示菜单时显示菜单,在不应该显示菜单时不显示菜单。
对于以下事件,您可以尝试
[[NSRunLoop currentRunLoop] runMode:NSEventTrackingRunLoopMode UntilDate:[NSDate distanceFuture]]
。 您将需要重复调用此函数,直到用户选择了菜单项之一。使用
nextEventMatchingMask:NSRightMouseUpMask
并不适用于所有情况,甚至大多数情况。 如果用户右键单击您的控件,鼠标右键将在下降后立即上升,而不选择菜单项,并且菜单项选择可能(但不一定)通过鼠标左键。 最好重复运行运行循环,直到用户选择某些内容或关闭菜单。我不知道如何判断用户已关闭菜单而未选择任何内容。
There is no direct equivalent, except in Carbon, which is deprecated.
For detecting the right-click, follow these instructions. They ensure that you will properly detect right-clicks and right-holds and display the menu when you should and not display it when you shouldn't.
For following events, you might try
[[NSRunLoop currentRunLoop] runMode:NSEventTrackingRunLoopMode untilDate:[NSDate distantFuture]]
. You will need to call this repeatedly until the user has chosen one of the menu items.Using
nextEventMatchingMask:NSRightMouseUpMask
will not work in all, or even most, cases. If the user right-clicks on your control, the right mouse button will go up immediately after it goes down, without selecting a menu item, and the menu item selection will probably (though not necessarily) happen through the left mouse button. Better to just run the run loop repeatedly until the user either selects something or dismisses the menu.I don't know how to tell that the user has dismissed the menu without selecting anything.