mouseMoved not called
Be sure to request the mouseMoved event is sent:
NSTrackingAreaOptions options = (NSTrackingActiveAlways | NSTrackingInVisibleRect |
NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved);
NSTrackingArea *area = [[NSTrackingArea alloc] initWithRect:[self bounds]
options:options
owner:self
userInfo:nil];
As noted by others, an NSTrackingArea
is a good solution, and an appropriate place to install the tracking area is NSView.updateTrackingAreas()
. It isn't necessary to set the containing NSWindow's setAcceptsMouseMovedEvents
property.
In Swift 3:
class CustomView : NSView {
var trackingArea : NSTrackingArea?
override func updateTrackingAreas() {
if trackingArea != nil {
self.removeTrackingArea(trackingArea!)
}
let options : NSTrackingAreaOptions =
[.mouseEnteredAndExited, .mouseMoved, .activeInKeyWindow]
trackingArea = NSTrackingArea(rect: self.bounds, options: options,
owner: self, userInfo: nil)
self.addTrackingArea(trackingArea!)
}
override func mouseMoved(with event: NSEvent) {
Swift.print("Mouse moved: \(event)")
}
}