gpt4 book ai didi

swift - 禁用应用程序包中的右键单击

转载 作者:行者123 更新时间:2023-11-30 11:42:55 27 4
gpt4 key购买 nike

我目前正在 mouseDrag 函数中接收 nextEvent

    while true {
guard let nextEvent = self.window?.nextEvent(matching: [.leftMouseDragged, .leftMouseUp, .rightMouseUp]) else {
continue
}
switch nextEvent.type {
case .leftMouseDragged:
self.cursorUpdate(with: nextEvent)
self.mouseDragged(with: nextEvent)

case .rightMouseUp:
continue

default:
self.mouseUp(with: nextEvent)
return
}
}

我只是想在这段时间内禁用右键单击。但是,通过此实现,右键单击只需排队并在循环完成后被调用。

如何禁用注册的右键单击?

最佳答案

尝试将 .rightMouseDown.rightMouseDragged 添加到匹配的蒙版中。正是 .rightMouseDown 事件使 AppKit 显示上下文菜单。

    guard let nextEvent = self.window?.nextEvent(matching: [.leftMouseDragged, .leftMouseUp, .rightMouseDown, .rightMouseDragged, .rightMouseUp]) else {
continue
}

您还可以考虑切换到 NSWindow.trackEvents(matching:timeout:mode:handler:) 而不是编写自己的循环:

    window.trackEvents(matching: [.leftMouseDragged, .leftMouseUp, .rightMouseDown, .rightMouseDragged, .rightMouseUp], timeout: NSEvent.foreverDuration, mode: .eventTrackingRunLoopMode) { (event, outStop) in
guard let event = event else { return }
switch event.type {
case .rightMouseDown, .rightMouseDragged, .rightMouseUp: return
case .leftMouseDragged:
self.cursorUpdate(with: event)
self.mouseDragged(with: event)
case .leftMouseUp:
self.mouseUp(with: event)
outStop.pointee = true
default:
break
}
}

使用 trackEvents 让 AppKit 运行主运行循环,因此计时器和观察者可以继续触发(如果它们被安排为 .eventTrackingRunLoopMode)。

关于swift - 禁用应用程序包中的右键单击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49140021/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com