Detect left and right mouse click on NSView

自作多情 提交于 2019-12-01 00:36:26

问题


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?


回答1:


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")
    }
}


来源:https://stackoverflow.com/questions/28201689/detect-left-and-right-mouse-click-on-nsview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!