简体   繁体   中英

Detect left and right mouse click on NSView

In Swift: I created a simple NSView and now want to execute different functions, depending on which mouseButton is pressed (left or right). how can I detect this?

You trap the corresponding mouseDown events

import Cocoa

class MyView : NSView {
    override func mouseDown(theEvent : NSEvent) {
        println("left mouse")
    }

    override func rightMouseDown(theEvent : NSEvent) {
        println("right mouse")
    }
}

See NSResponder for more magic.

Swift 4

import Cocoa

class MyView : NSView {
    override func mouseDown(with theEvent: NSEvent) {
        print("left mouse")
    }

    override func rightMouseDown(with theEvent: NSEvent) {
        print("right mouse")
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM