提问人:Haren 提问时间:9/7/2023 最后编辑:jnpdxHaren 更新时间:9/7/2023 访问量:61
如何使 WKWebView 中的当前网页在不聚焦时接收鼠标移动(关键窗口,在 macOS 上)
How to make current webpage in WKWebView to receive mouse movements when it isn't focused (key window, on macOS)
问:
我知道这种情况很少见,但是,我别无选择。
我创建了一个 NSWindow 以动态壁纸的形式显示在桌面上,将其 styleMask 设置为隐藏其标题栏,并将其级别设置为 ,其 contentView 中有一个 WKWebView。.borderless
NSWindow.Level(Int(CGWindowLevelForKey(.desktopWindow)))
这意味着它不能成为关键窗口或主窗口。更重要的是,窗口的交互空间被桌面上的Finder应用程序挡住了。
我的方法是用于观察全局键盘和鼠标事件,并通过调用 .NSEvent.addGlobalMonitorForEvents
webview.mouseMoved(event)
func setEventHandler() {
self.eventHandler = NSEvent.addGlobalMonitorForEvents(matching: .any) { [weak self] event in
// contentView.subviews.first -> SwiftUIView.subviews.first -> WKWebView
if let webview = self?.wallpaperWindow.contentView?.subviews.first?.subviews.first,
let frontmostApplication = NSWorkspace.shared.frontmostApplication,
webview is WKWebView,
frontmostApplication.bundleIdentifier == "com.apple.finder" {
switch event.type {
case .scrollWheel:
var modified = event
webview.scrollWheel(with: event)
case .mouseMoved:
webview.mouseMoved(with: event)
case .mouseEntered:
webview.mouseEntered(with: event)
case .mouseExited:
webview.mouseExited(with: event)
case .leftMouseUp:
fallthrough
case .rightMouseUp:
webview.mouseUp(with: event)
case .leftMouseDown:
webview.mouseDown(with: event)
// case .rightMouseDown:
// view?.mouseDown(with: event)
case .leftMouseDragged:
fallthrough
case .rightMouseDragged:
webview.mouseDragged(with: event)
default:
break
}
}
}
}
但它并不完美,鼠标移动仍然被忽略。
所以我想知道是否有一种方法可以让 WKWebView 处理它接收到网页的鼠标事件,而不是作为关键窗口,比如悬停效果等等。
答: 暂无答案
评论
webview.mouseMoved(with: event)
webview.mouseMoved(with: event)
webview.mouseMoved(with: event)
WKWebView.mouseMoved(with:)