swiftmouseupnsslider

Detect mouseUp event from NSSlider in Swift 4 / MacOS


I'm pretty new to Swift, so please have mercy on me.

I have a function that I want to call when the user has finished sliding an NSSlider object. My natural thought is to use the mouseUp event. However, lots of Googling and reading documentation has left me stranded. What am I missing here?


    @IBOutlet weak var tSlider: NSSlider!
    @IBOutlet weak var tLabel: NSTextField!

    @IBAction func sliderValueChanged(_ sender: NSSlider) {

        if let label = self.value(forKey: "tLabel") as? NSTextField {

            label.intValue = sender.intValue
            t_value = Int8(sender.intValue)

        }

        appDelegate.sendValue()

        Swift.print("Value Changed")

        // Detect Mouse Up

        override func mouseUp(with theEvent: NSEvent) {

             appDelegate.sendHIDValue()

             Swift.print("Mouse Up")

         }

    }

Nothing happens when I release my mouse button from the slider. However, if I mouse up in the window itself, I do get a mouse up event. Specifically, how might I get the mouse up event from the NSSlider?


Solution

  • Thank you @Willeke for posting that possible duplicate. With the information contained therein, I was able to solve this for myself. For anyone else who is interested in this topic, here is my solution that's been updated for Swift 4:

    
    
        @IBAction func sliderValueChanged(_ sender: NSSlider) {
    
            let event = NSApplication.shared.currentEvent
    
            if event?.type == NSEvent.EventType.leftMouseUp {
    
            // Do something
    
            }
    
        }