2017-05-24 119 views
0

我一直在尝试点击鼠标。我可以用鼠标左键单击获取原始值等。我现在想添加鼠标右键单击。我已经设置了一个基本的例子。如果只有一个鼠标右键单击,它会执行一个功能,如果有两个鼠标右键单击,它会执行不同的功能。问题是,如果你做了两次鼠标点击,它显然无法区分这两者,因此在执行第二次鼠标功能之前触发一次鼠标点击功能。 我正在考虑使用某种计时器来记录点击次数。但是我最终会围着圈子走,因为我似乎一次又一次地启动了定时器。我希望有人可以帮助。感谢您在这里阅读的代码。阅读鼠标点击的次数点击次数

的Xcode 8斯威夫特3的Mac OSX塞拉利昂NOT IOS ..非IOS

import Cocoa 

class MainWindowController: NSWindowController { 

    @IBOutlet weak var MyView: NSView! 

    override func windowDidLoad() { 
     super.windowDidLoad() 

     //Initialize mouse for Right Click numberOfClicksRequired = 1 
     let recogRightClick1 = NSClickGestureRecognizer(target: self, action: #selector(oneMouseClick)) 
     recogRightClick1.buttonMask = 0x2 
     recogRightClick1.numberOfClicksRequired = 1 
     MyView.addGestureRecognizer(recogRightClick1) 

     //Initialize mouse for Right ClicknumberOfClicksRequired = 2 
     let recogRightClick2 = NSClickGestureRecognizer(target: self, action: #selector(twoMouseClick(myrec:myRightClick:))) 
     recogRightClick2.buttonMask = 0x2 
     recogRightClick2.numberOfClicksRequired = 2 
     MyView.addGestureRecognizer(recogRightClick2) 
    }//EO Overide 


    func oneMouseClick(myrec: NSPanGestureRecognizer,myRightClick:NSClickGestureRecognizer){ 
     let rightClick = myRightClick.state.rawValue 
     print("oneMouseClick",rightClick) 
    } 


    func twoMouseClick(myrec: NSPanGestureRecognizer,myRightClick:NSClickGestureRecognizer){ 
     let rightClick = myRightClick.state.rawValue 
     print("twoMouseClick",rightClick) 
    } 


}//EnD oF thE wORld 

UPDATE

我已经重新生成下面给出的建议代码。现在,代码更多地反映了我想要做的事情。我唯一的问题是,我希望所有的鼠标操作只能在'myView'内而不是在主窗口内触发。我认为这可能与第一响应者有关,但似乎并不奏效。再次,任何想法将不胜感激。请原谅我自学的错误代码。

的Xcode 8斯威夫特3的Mac OSX塞拉利昂NOT IOS ..非IOS

import Cocoa 

class MainWindowController: NSWindowController { 

    @IBOutlet weak var myView: NSView! 
    var mouseX:CGFloat = 0 
    var mouseY:CGFloat = 0 

    override func windowDidLoad() { 
     myView.window?.becomeFirstResponder() 
     myView.window?.acceptsMouseMovedEvents = true 
     super.windowDidLoad() 

     myView.wantsLayer = true 
     myView.layer?.backgroundColor = CGColor(red: 0.05, green: 0.57, blue: 0.80, alpha: 0.6) 

     NSEvent.addLocalMonitorForEvents(matching:.leftMouseDown){ 
      self.mouseEventFunction(data: 1) 
      return $0 
     } 

     NSEvent.addLocalMonitorForEvents(matching:.leftMouseUp){ 
      self.mouseEventFunction(data:2) 
      return $0 
     } 

     NSEvent.addLocalMonitorForEvents(matching:.rightMouseDown){ 
      self.mouseEventFunction(data: 3) 
      return $0 
     } 

     NSEvent.addLocalMonitorForEvents(matching:.rightMouseUp){ 
      self.mouseEventFunction(data: 4) 
      return $0 
     } 


     NSEvent.addLocalMonitorForEvents(matching:.mouseMoved) { 
      self.mouseX = NSEvent.mouseLocation().x 
      self.mouseY = NSEvent.mouseLocation().y 
      return $0 } 

    }//EO Overide 



    func mouseEventFunction (data: Int){ 
     switch data{ 
     case 1 : 
      print("LeftMouseDown") 
     case 2 : 
      print("LeftMouseUp") 
     case 3 : 
      print("RightMouseDown") 
     case 3 : 
      print("RightMouseUP") 
     default: break 
        } 

     if data == 1 {print("mouseX",mouseX,"mouseY",mouseY)} 

    }//eo mouseEvent 



}//EnD oF thE wORld 

更新2 现在我已经更新子类视图控制器,所以鼠标点击现在只能在MyView的工作。我仍然有'func mouseDragged'的问题我需要实现的是我的视图的左下角是x = 0和Y = 0。我尝试了转换,但那不工作。希望有人能指导我。感谢阅读这里是更新的代码。

的Xcode 8斯威夫特3的Mac OSX塞拉利昂NOT IOS ..非IOS

import Cocoa 

class MainWindowController: NSWindowController { 

    @IBOutlet weak var myView: NSView! 

    override func windowDidLoad() { 
     myView.window?.becomeFirstResponder() 
     myView.window?.acceptsMouseMovedEvents = true 
     window?.contentView?.addSubview(myView) 
     super.windowDidLoad() 

    }//EO Overide 


}//EnD oF thE wORld 


class testView: NSView { 

    override func draw(_ dirtyRect: NSRect) { 
     let backgroundColor = NSColor.lightGray 
     backgroundColor.set() 
     NSBezierPath.fill(bounds) 
    } 

    override func mouseDragged(with theEvent: NSEvent) { 
     let myLocationInWindow = theEvent.locationInWindow 
     let location = convert(myLocationInWindow, to: self) 
     Swift.print("myLocationInWindow",myLocationInWindow,"location",location) 
    } 

    override func mouseDown(with theEvent: NSEvent) { 
     Swift.print("mouseDown") 
    } 

    override func mouseUp(with theEvent: NSEvent) { 
     Swift.print("mouseUp clickCount: \(theEvent.clickCount)") 
    } 


}//eo testView 
+0

Ther e是一个简单得多的方法:在需要的地方重写'func mouseDown(event:NSEvent)',并且在这个方法中,执行'if event.clickCount == 2 {stuff} else {other stuff}'。如有必要,添加'super.mouseDown(with:event)'。 – Moritz

+0

你为什么继承窗口而不是视图控制器? –

+0

我已经重新生成的代码,并添加到原来的帖子 – oldman

回答

0

要定义鼠标内部查看您使用

let myLocationInWindow = theEvent.locationInWindow 
let location = convert(myLocationInWindow, from: nil) 

,其中零是窗口

这里是最终代码

import Cocoa 

class MainWindowController: NSWindowController { 

    @IBOutlet weak var myView: NSView! 

    override func windowDidLoad() { 
     super.windowDidLoad() 
    }//EO Overide 


}//EnD oF thE wORld 


class testView: NSView { 

    override func draw(_ dirtyRect: NSRect) { 
     let backgroundColor = NSColor.lightGray 
     backgroundColor.set() 
     NSBezierPath.fill(bounds) 
    } 

    override func mouseDragged(with theEvent: NSEvent) { 
     let myLocationInWindow = theEvent.locationInWindow 
     let location = convert(myLocationInWindow, from: nil) 
     Swift.print("location",location) 
    } 

    override func mouseDown(with theEvent: NSEvent) { 
     Swift.print("mouseDown") 
    } 

    override func mouseUp(with theEvent: NSEvent) { 
     Swift.print("mouseUp clickCount: \(theEvent.clickCount)") 
    } 

}//eo testView