Reputation: 5624
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).
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.
Upvotes: 2
Views: 1136
Reputation: 53551
To convert from window coordinates you have to use convertPoint:fromView:
with nil
as the view parameter. Your conversion does the opposite.
Upvotes: 4