Grant Wilkinson
Grant Wilkinson

Reputation: 1128

mouseMoved event for NSView

In my app delegate I created a window "helpWindow" and I set its contents view to a NSView subclass. In my subclass I drawRect and make sure its the key window. The problem I am having is that on my mouse events the mouse down event works fine with the content view however, the mouse moved is not working and displaying the location. Do i have to add something to the mouseLocation ? I feel that the drawRect is covering up the mouse moved event. Thanks!

//in my appDelegate.m

controlFilterBox = [[MoveFilter alloc] initWithFrame:helpWindow.frame];
[helpWindow setContentView:controlFilterBox];
[controlFilterBox release];

//in my NSView subclass.m

   -(void)drawRect:(NSRect)dirtyRect 
     {
        [[NSColor redColor] set];
        NSRectFill(dirtyRect);

        [[self window] makeKeyWindow]; 
     }

    -(void)mouseDown:(NSEvent *)theEvent 
      {

        NSPoint eventLocation = [theEvent locationInWindow];
        NSPoint location = [self convertPoint:eventLocation fromView:nil];

        NSLog(@"exit %f %f", location.x, location.y); 
      }

    -(void)mouseMoved:(NSEvent *)theEvent 
      {
        NSPoint mouseLoc = [NSEvent mouseLocation];
        NSLog(@"mouseMoved: %f %f", mouseLoc.x, mouseLoc.y);
      }

Upvotes: 2

Views: 1921

Answers (1)

Grant Wilkinson
Grant Wilkinson

Reputation: 1128

I found the answer, it turns out it is best to create an NSTrackingArea to be able to use the mouseMoved event in a NSView.

Upvotes: 2

Related Questions