NSMenu阻塞定时器/全局事件监视器/等

时间:2022-11-10 00:01:33

I'm showing a popup menu when clicking a button. The problem is that, when that menu is open, timers no longer work:

单击按钮时我会显示一个弹出菜单。问题是,当该菜单打开时,定时器不再起作用:

To show the menu:

要显示菜单:

[_menu popUpMenuPositioningItem:nil atLocation:point inView:nil];

My timer (called before the popup) is no longer sending messages after the button is clicked.

单击按钮后,我的计时器(在弹出窗口之前调用)不再发送消息。

[NSTimer scheduledTimerWithTimeInterval:0.50
                                 target:self
                               selector:@selector(myMethod:)
                               userInfo:nil
                                repeats:YES];

Have also tried [NSEvent addGlobalMonitorForEventsMatchingMask..] and even CGEventTapCreate. Nothing gets called.

还试过[NSEvent addGlobalMonitorForEventsMatchingMask ..]甚至CGEventTapCreate。什么都没有被调用。

How do I make the popup not-blocking / modal? I know this is possible, I've seen another app do it. Thanks for any ideas.

如何使弹出窗口无阻塞/模态?我知道这是可能的,我见过另一个应用程序。谢谢你的任何想法。

1 个解决方案

#1


1  

The problem may be that the NSTimer is not running on the right mode. Try grabbing the current run loop by creating a NSRunLoop object. Then add your timer to the NSModalPanelRunLoopMode using the method addTimer:forMode: (in the run loop object you just created). You can read more about it in the Apple Docs here: https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSRunLoop_Class/Reference/Reference.html

问题可能是NSTimer没有在正确的模式下运行。尝试通过创建NSRunLoop对象来抓取当前的运行循环。然后使用addTimer:forMode :(在刚刚创建的运行循环对象中)方法将计时器添加到NSModalPanelRunLoopMode。您可以在Apple Docs中阅读更多相关信息:https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSRunLoop_Class/Reference/Reference.html

#1


1  

The problem may be that the NSTimer is not running on the right mode. Try grabbing the current run loop by creating a NSRunLoop object. Then add your timer to the NSModalPanelRunLoopMode using the method addTimer:forMode: (in the run loop object you just created). You can read more about it in the Apple Docs here: https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSRunLoop_Class/Reference/Reference.html

问题可能是NSTimer没有在正确的模式下运行。尝试通过创建NSRunLoop对象来抓取当前的运行循环。然后使用addTimer:forMode :(在刚刚创建的运行循环对象中)方法将计时器添加到NSModalPanelRunLoopMode。您可以在Apple Docs中阅读更多相关信息:https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSRunLoop_Class/Reference/Reference.html