我已经用以下方法创建了水龙头:
guard let eventTap = CGEvent.tapCreate(tap: .cgSessionEventTap,
place: .headInsertEventTap,
options: .defaultTap,
eventsOfInterest: CGEventMask(eventMask),
callback: myCGEventCallback,
userInfo: nil) else {
logger.info("failed to create event tap")
exit(1)
}
我的应用程序按预期工作。我的应用程序也可能在以下任何时候锁定机器tapCreate()
:
let task = Process()
task.arguments = ["-suspend"]
task.launchPath = "/System/Library/CoreServices/Menu Extras/User.menu/Contents/Resources/CGSession"
task.launch()
使用上述代码进入登录屏幕并重新登录后,上面创建的水龙头将通过此exit关闭。
我一直没有成功找到登录挂钩或tap
在用户重新登录后重新创建我的方法。tap
在这种情况下如何重新启动?
找到了一种在用户重新登录时获得通知的方法。这仅在启动应用程序的用户重新登录时触发。在 中applicationDidFinishLaunching
,我添加了:
NSWorkspace.shared().notificationCenter.addObserver(self, selector: #selector(self.rebuildEventChains(aNotification:)), name: NSNotification.Name.NSWorkspaceSessionDidBecomeActive, object: nil);
登录时调用的函数:
func rebuildEventChains(aNotification : NSNotification){
// re-enable the tap if is disabled
// ...
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句