在appkit中禁用右键单击

问题描述 投票:0回答:1

我目前在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
        }
    }

而我只是试图在持续时间内禁用右键单击。但是,通过此实现,右键单击只是排队并在循环结束后调用。

如何禁用正确的点击注册?

swift macos appkit nsresponder
1个回答
0
投票

尝试将.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)。

© www.soinside.com 2019 - 2024. All rights reserved.