Reputation: 487
I thought I would post a question and answer regarding continuously tracking the mouse location in cocoa ( in OSX 10.5.8 using xcode 3.1.4) within a cocoa app.
It is a self answer question because I finally figured it out and couldn't find an answer here that I thought described how I managed to solve it
And possibly some people might think its easier than using event taps plus without the masking features in 10.6 this is pretty easy :)
Upvotes: 0
Views: 2190
Reputation: 6090
Thanks! and I got the global mouse coordinates with [NSEvent mouseLocation];
Upvotes: 0
Reputation: 487
heres how I did it.
First of all this is the code I used to set up the tracking area :
NSTrackingArea* trackingArea = [ [ NSTrackingArea alloc] initWithRect:[ mainWindow frame] options:(NSTrackingMouseMoved | NSTrackingActiveAlways ) owner:self userInfo:nil);
[container addTrackingArea:trackingArea];
When I create the main window I add this message while i'm setting everything up :
[window setAcceptsMouseMovedEvents:YES];
and in the header file for the class that I want to catch the event (which for me is in the same class as I created the tracking area ) I subclass NSResponder and then implement the event I want to catch which for me is mouseMoved.
-(void) mouseMoved: (NSEvent *) thisEvent
{
NSPoint cursorPoint = [ thisEvent locationInWindow ];
NSLog(@"X coordinate is %f and Y coordinate is %f",cursorPoint.x,cursorPoint.y);
}
Hope this is of some help :)
Thanks!
Upvotes: 1