Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 737
ixany Avatar asked Jan 28 '15 20:01

ixany


1 Answers

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")
    }
}
like image 90
Warren Burton Avatar answered Oct 19 '22 11:10

Warren Burton