我一直在尝试点击鼠标。我可以通过鼠标左键单击获取原始值等等。我现在想要添加鼠标右键单击。我已经设置了一个基本的例子。如果有一个鼠标右键单击它执行一个功能我想要实现,如果有两个鼠标右键单击它执行不同的功能。问题是如果你做两次鼠标点击它显然无法区分两者,因此在执行第二个鼠标功能之前触发一个鼠标单击功能。 我在考虑使用某种计时器来记录点击次数。但我最终绕圈子,因为我似乎一遍又一遍地启动计时器。我希望有人可以提供帮助。感谢您阅读此处的代码。
Xcode 8 Swift 3 Mac OSX Sierra不是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
更新
我按照给出的建议重新生成了代码。现在代码反映了我想要做的更多。我唯一的问题是我希望所有的鼠标操作都只能在myView'内部触发。而不是在主窗口内。我认为它可能与第一响应者有关但但似乎没有用。再次任何想法将不胜感激。请原谅我自学的任何不良代码。
Xcode 8 Swift 3 Mac OSX Sierra不是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 我现在更新了subClassing视图控制器,因此鼠标点击现在只能在myView中使用。我仍然遇到过&func; mouseDragged'我需要实现的是我视图的左下角是x = 0和Y = 0.我尝试过转换,但那不起作用。希望有人可以指导我。感谢您在这里阅读更新的代码。
Xcode 8 Swift 3 Mac OSX Sierra不是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 :(得分:1)
要在视图中定义鼠标,请使用
let myLocationInWindow = theEvent.locationInWindow
let location = convert(myLocationInWindow, from: nil)
其中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