2
votes

I have a custom view class, which receives the mouse events. I do not get correct coordinates of the mouseDown or mouseUp events. No matter where I click inside the view, I get the same incorrect coordinates.

This is the function I have in my Custom NSView class

- (void)mouseDown:(NSEvent *)theEvent
{
    NSPoint coordinateInBaseWindow = theEvent.locationInWindow;
    
    NSPoint coordinateInView = [self convertPoint:coordinateInBaseWindow fromView:nil];
    NSLog(@"left mouse down (x,y) =(%f,%f)", coordinateInView.x, coordinateInView.y);
}

I have a similar handler of mouseUp event.

Now the output is always

left mouse down (x,y) =(0.000000,-1.996910)

left mouse up (x,y) =(0.000000,0.000000)

No matter where I click inside the view, the coordinate of left mouse down is always (0.000000,-1.996910) and left mouse up is always (0.000000,0.000000).

  1. What am I doing wrong? Why I am unable to get correct coordinates of the mouse click event?
  2. mouseDown and mouseUp together makes one click. Shouldn't the coordinates of the mouseDown and mouseUp equal? Why are they different in this case?

Edit 1:

As omz pointed out, I should have used convertPoint:fromView: but this too has not solved the problem. I am still getting the same coordinates.

Edit 2: My format specifiers in NSLog were incorrect which were giving me totally wrong results.

1

1 Answers

4
votes

To convert from window coordinates you have to use convertPoint:fromView: with nil as the view parameter. Your conversion does the opposite.