我一直在嘗試點擊鼠標。我可以用鼠標左鍵單擊獲取原始值等。我現在想添加鼠標右鍵單擊。我已經設置了一個基本的例子。如果只有一個鼠標右鍵單擊,它會執行一個功能,如果有兩個鼠標右鍵單擊,它會執行不同的功能。問題是,如果你做了兩次鼠標點擊,它顯然無法區分這兩者,因此在執行第二次鼠標功能之前觸發一次鼠標點擊功能。 我正在考慮使用某種計時器來記錄點擊次數。但是我最終會圍着圈子走,因爲我似乎一次又一次地啓動了定時器。我希望有人可以幫助。感謝您在這裏閱讀的代碼。閱讀鼠標點擊的次數點擊次數
的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
Ther e是一個簡單得多的方法:在需要的地方重寫'func mouseDown(event:NSEvent)',並且在這個方法中,執行'if event.clickCount == 2 {stuff} else {other stuff}'。如有必要,添加'super.mouseDown(with:event)'。 – Moritz
你爲什麼繼承窗口而不是視圖控制器? –
我已經重新生成的代碼,並添加到原來的帖子 – oldman