I've got a custom ViewController
, a custom WindowController
and a custom View
. The ViewController
is connected to the WindowController
and the View
is connected to the ViewController
.
In my ViewController
I have the following code for the mouse events:
override func mouseDown(with event: NSEvent) {
//code
}
override func mouseUp(with event: NSEvent) {
//code
}
On launching this, I'm getting the following error message and furthermore on debugging these methods are not even entered:
2018-03-08 11:27:11.594158+0100 MyProject[2697:83761] *** Assertion failure in -[NSEvent touchesMatchingPhase:inView:], /Library/Caches/com.apple.xbs/Sources/AppKit/AppKit-1504.83.101/AppKit.subproj/NSEvent.m:4654
2018-03-08 11:27:11.594574+0100 MyProject[2697:83761] [General] Invalid message sent to event "NSEvent: type=LMouseDown loc=(701.219,422.156) time=8788.4 flags=0 win=0x6080001e0200 winNum=1255 ctxt=0x0 evNum=1071 click=1 buttonNumber=0 pressure=1 deviceID:0x300000014400000 subtype=NSEventSubtypeTouch"
2018-03-08 11:27:11.599387+0100 MyProject[2697:83761] [General] (
0 CoreFoundation 0x00007fff8b59d2cb __exceptionPreprocess + 171
1 libobjc.A.dylib 0x00007fffa03b548d objc_exception_throw + 48
2 CoreFoundation 0x00007fff8b5a2042 +[NSException raise:format:arguments:] + 98
3 Foundation 0x00007fff8cfeabe0 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 195
4 AppKit 0x00007fff89327573 -[NSEvent touchesMatchingPhase:inView:] + 205
5 MyProject 0x0000000100013c0b _T015MyProject18MyViewControllerC9mouseDownySo7NSEventC4with_tF + 139
6 MyProject 0x0000000100014bbc _T015MyProject18MyViewControllerC9mouseDownySo7NSEventC4with_tFTo + 60
7 AppKit 0x00007fff891897a9 forwardMethod + 133
8 AppKit 0x00007fff891897a9 forwardMethod + 133
9 AppKit 0x00007fff8990424f -[NSWindow(NSEventRouting) _handleMouseDownEvent:isDelayedEvent:] + 6341
10 AppKit 0x00007fff89900a6c -[NSWindow(NSEventRouting) _reallySendEvent:isDelayedEvent:] + 1942
11 AppKit 0x00007fff898fff0a -[NSWindow(NSEventRouting) sendEvent:] + 541
12 AppKit 0x00007fff89784681 -[NSApplication(NSEvent) sendEvent:] + 1145
13 AppKit 0x00007fff88fff427 -[NSApplication run] + 1002
14 AppKit 0x00007fff88fc9e0e NSApplicationMain + 1237
15 MyProject 0x0000000100023add main + 13
16 libdyld.dylib 0x00007fffa0c9b235 start + 1
)
I've already tried the following things:
Register these both methods in the WindowController and just write in each:
let vc = self.nextResponder as! MyViewController vc.mouseUp // or vc.mouseDown
--> still getting the above error
Register these both methods in the viewDidLoad() / windowDidLoad()
by writing the following Code:
NSEvent.addLocalMonitorForEvents(matching: .leftMouseDown){
self.mouseDown(with: $0)
return $0
}
NSEvent.addLocalMonitorForEvents(matching: .leftMouseUp){
self.mouseUp(with: $0)
return $0
}
--> still getting the above error
Question: How can I make these both methods work in my ViewController?
Important Note: Please consider that I am using Swift 4.0.2
and Xcode 9.1 Build 9B55
The error:
Assertion failure in -[NSEvent touchesMatchingPhase:inView:]
Invalid message sent to event "NSEvent: type=LMouseDown
touches(matching:in:)
is called from mouseDown(with:)
. The documentation of touches(matching:in:)
says:
This method is only valid for gesture events (gesture, magnify, swipe, rotate, etc.).