popUpMenuPositioningItem:atLocation:inView: hangs when switching to another application
我正在开发无坞站应用程序(
我的问题是,每当用户切换到另一个应用程序时(使用?-TAB),所有以编程方式弹出菜单的方法都将挂起,而无需先关闭菜单。我已经尝试过
如果用户使用ESC键关闭菜单,则一切正常,但是,如果用户切换到其他应用程序,则上述方法将永远不会返回(它们似乎同步运行并在关闭菜单时返回)。该应用程序不会崩溃,并且有一些技巧可以重新获得控制权(调用曝光或单击弹出菜单的任何
如果应用程序具有停靠图标(即将
这是使用键盘快捷键时弹出菜单的代码:
1 2 3 | [mainMenu popUpMenuPositioningItem:[mainMenu itemAtIndex:0] atLocation:[NSEvent mouseLocation] inView:nil]; |
这是单击
1 2 3 4 | - (void)mouseDown:(NSEvent *)event { [statusItem popUpStatusItemMenu:[statusItem menu]]; } |
关于如何解决此问题的任何想法?
感谢您的帮助。
更新
此问题还与激活的应用程序有关(我在显示菜单之前使用
问题似乎是由于应用程序激活和popUpMenu在同一事件周期中发生的。我找到了一种解决方法,在这篇文章中有更详细的描述。
是摘要,您需要先激活应用程序(使用
1 2 3 4 5 6 7 8 9 10 11 | - (void) someMethod { // Some code [NSApp activateIgnoringOtherApps:YES]; [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(showMenu) userInfo:nil repeats:NO]; //Some other code } - (void) showMenu { // This will show the menu at the current mouse position [aMenu popUpMenuPositioningItem:[mainMenu itemAtIndex:0] atLocation:[NSEvent mouseLocation] inView:nil]; } |