使用NSDistributedNotificationCenter和CFRunLoopAddSource时,Macos应用程序挂起
我使用Qt构建了MacOS应用程序。在那里,我创建了NSDistributedNotificationCenter,以便在辅助功能设置更改时收到通知(注意“com.apple.accessibility.api”)。我还有CFRunLoopAddSource来监视按键事件 但是,当我运行程序并更改可访问性时,应用程序挂起,无法正常运行 有人能帮忙看看为什么会这样吗 代码如下: 在这里,我创建了一个观察者: 创建/删除由单击按钮控制使用NSDistributedNotificationCenter和CFRunLoopAddSource时,Macos应用程序挂起,macos,cocoa,nsnotificationcenter,cgeventtap,cfrunloop,Macos,Cocoa,Nsnotificationcenter,Cgeventtap,Cfrunloop,我使用Qt构建了MacOS应用程序。在那里,我创建了NSDistributedNotificationCenter,以便在辅助功能设置更改时收到通知(注意“com.apple.accessibility.api”)。我还有CFRunLoopAddSource来监视按键事件 但是,当我运行程序并更改可访问性时,应用程序挂起,无法正常运行 有人能帮忙看看为什么会这样吗 代码如下: 在这里,我创建了一个观察者: 创建/删除由单击按钮控制 - (void)createObserver { [[NSDis
- (void)createObserver
{
[[NSDistributedNotificationCenter defaultCenter] addObserver:self
selector:@selector(didToggleAccessStatus:)
name:@"com.apple.accessibility.api"
object:nil
suspensionBehavior:NSNotificationSuspensionBehaviorDeliverImmediately];
}
这就是我添加按键事件记录器的方式:
// Create an event tap to retrieve keypresses.
CGEventMask eventMask = (CGEventMaskBit(kCGEventKeyDown) |
CGEventMaskBit(kCGEventFlagsChanged) |
CGEventMaskBit(kCGEventLeftMouseDown) |
CGEventMaskBit(kCGEventRightMouseDown) |
CGEventMaskBit(kCGEventMouseMoved) |
CGEventMaskBit(kCGEventScrollWheel));
//| CGEventMaskBit(kCGEventLeftMouseDragged)
//| CGEventMaskBit(kCGEventRightMouseDragged)
//| CGEventMaskBit(kCGEventOtherMouseDragged););
CFMachPortRef m_eventTap = nullptr;
CFRunLoopSourceRef m_runLoopSource = nullptr;
m_eventTap = CGEventTapCreate(
kCGSessionEventTap, kCGHeadInsertEventTap, kCGEventTapOptionDefault,
eventMask, myCGEventCallback, nullptr);
if (m_eventTap != Q_NULLPTR) {
NSLog(@"CGEventTap created");
// Create a run loop source and add enable the event tap.
m_runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, m_eventTap, 0);
CFRunLoopAddSource(CFRunLoopGetCurrent()/*CFRunLoopGetMain()*/, m_runLoopSource, kCFRunLoopCommonModes);
CGEventTapEnable(m_eventTap, true);
//CFRunLoopRun();
}
else {
m_runLoopSource = Q_NULLPTR;
NSLog(@"Error creating CGEventTap");
}
有什么想法吗
提前感谢并注意当新的RunLoop处于活动状态时,挂起听起来像Qt的事件循环没有得到处理。什么新的RunLoop?如果我只创建键盘记录器,一切正常,我认为Qt事件循环也是活动的“应用程序挂起”-挂起做什么?看看堆栈跟踪。实际上不是真正的挂起。鼠标和键盘停止响应,我必须重新启动Laptop什么新的RunLoop?-CFRunLoopRun()停止响应时是否已注释掉?