Search code examples
cocoaevent-handlingnsviewappkitmouseup

NSView does not receive mouseUp: event when mouse button is released outside of view


I have a custom NSView subclass with (for example) the following methods:

override func mouseDown(with event: NSEvent) { Swift.print("mouseDown") }
override func mouseDragged(with event: NSEvent) { Swift.print("mouseDragged") }
override func mouseUp(with event: NSEvent) { Swift.print("mouseUp") }

As long as the mouse (button) is pressed, dragged and released all inside the view, this works fine. However, when the mouse is depressed inside the view, moved outside the view, and only then released, I never receive the mouseUp event.

P.S.: Calling the super implementations does not help.


Solution

  • The Handling Mouse Dragging Operations section of Apple's mouse events documentation provided a solution: Apparently, we do receive the mouseUp event when tracking events with a mouse-tracking loop.

    Here's a variant of the sample code from the documentation, adapted for Swift 3:

    override func mouseDown(with event: NSEvent) {
        var keepOn = true
    
        mouseDownImpl(with: event)
    
        // We need to use a mouse-tracking loop as otherwise mouseUp events are not delivered when the mouse button is
        // released outside the view.
        while true {
            guard let nextEvent = self.window?.nextEvent(matching: [.leftMouseUp, .leftMouseDragged]) else { continue }
            let mouseLocation = self.convert(nextEvent.locationInWindow, from: nil)
            let isInside = self.bounds.contains(mouseLocation)
    
            switch nextEvent.type {
            case .leftMouseDragged:
                if isInside {
                    mouseDraggedImpl(with: nextEvent)
                }
    
            case .leftMouseUp:
                mouseUpImpl(with: nextEvent)
                return
    
            default: break
            }
        }
    }
    
    func mouseDownImpl(with event: NSEvent) { Swift.print("mouseDown") }
    func mouseDraggedImpl(with event: NSEvent) { Swift.print("mouseDragged") }
    func mouseUpImpl(with event: NSEvent) { Swift.print("mouseUp") }