如何在 Swift 中使用 AXObserverAddNotification 来检测 UI 更改?
Obj C 中有一个很好的答案。
我的应用程序如何检测另一个应用程序窗口的更改? https://stackoverflow.com/questions/853833/how-can-my-app-detect-a-change-to-another-apps-window
如何将答案翻译成 Swift?
下面的测试代码编译并运行没有错误,但是当我更改主窗口时,callBack 似乎没有被执行。
func observe(app:pid_t, element:AXUIElement) {
var observer: AXObserver? = nil
func callBack(observer: AXObserver?, element: AXUIElement?, notification: CFString, refcon: UnsafeMutableRawPointer?) {
print("Fired! \(notification)")
}
if AXObserverCreate(app, callBack, &observer) == .success {
print("AXObserverCreate success!")
if AXObserverAddNotification(observer!, element, kAXMainWindowChangedNotification as CFString, UnsafeMutableRawPointer(Unmanaged.passRetained(self).toOpaque())) == .success {
print("AXObserverAddNotification success!")
CFRunLoopAddSource(CFRunLoopGetCurrent(), AXObserverGetRunLoopSource(observer!), .defaultMode)
print("Watching \(element.value(of:"AXTitle"))")
}
}
}
@kyo-ago https://stackoverflow.com/users/9762269/kyo-ago在将那些类似 C 的 swift API 封装在他的代码中方面做得非常出色project https://github.com/kyo-ago/Automatically-Move-Mouse-Pointer-To-Default-Button-in-a-dialog-box-for-mac/blob/master/Automatically%20Move%20Mouse%20Pointer%20To%20Default%20Button%20in%20a%20dialog%20box%20for%20mac/Applications/Application.swift.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)