【问题标题】:Disabling right click in appkit在appkit中禁用右键单击
【发布时间】:2018-03-06 21:11:43
【问题描述】:

我目前正在我的 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】:

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

【讨论】:

    猜你喜欢
    • 2016-05-04
    • 1970-01-01
    • 1970-01-01
    • 2013-10-05
    • 2012-07-01
    • 1970-01-01
    • 2014-05-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多