How can I call my own method when the area defined by NSTrackingArea captures mouse events? Can I specify my own method (e.g. "myMethod") in the NSTrackingArea init as below?
trackingAreaTop = [[NSTrackingArea alloc] initWithRect:NSMakeRect(0,0,100,100) options:(NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved | NSTrackingActiveAlways) owner:myMethod userInfo:nil];
Thanks!
No. The
owneris supposed to be an object to receive the requested mouse-tracking, mouse-moved, or cursor-update messages, not a method. It won't even compile if you pass in your custom method.NSTrackingAreaonly defines the area that is sensitive to the movements of the mouse. In order to respond to them, you need to:addTrackingArea:method.mouseEntered:,mouseMoved:ormouseExited:methods in yourownerclass as per your need.For details, please refer to Using Tracking-Area Objects.
As a aside, if you want to respond to mouse clicking events rather then mouse movement events, there is no need to use
NSTrackingArea. Just go ahead and implement themouseDown:ormouseUp:method instead.