John Smith
John Smith

Reputation: 861

How to get real mouse position inside window's client on Mac OSX?

When I use this code I have a wrong value of Y-mouse position. The returned value is (x, y+20).

int Height = int [[Window contentView] frame].size.height;

int x = (int)[(NSEvent *)event locationInWindow].x;

int y = Height - (int)[(NSEvent *)event locationInWindow].y;

I think that Height is the height of window :`title bar + rect client. I would like the real value of Height;

Upvotes: 0

Views: 460

Answers (1)

Pat_Morita
Pat_Morita

Reputation: 3535

You can convert the coordinates to a view.

   - (void)mouseMoved:(NSEvent *)event
    {
        NSPoint locationInView = [self convertPoint:[event locationInWindow]
                                           fromView:yourView];
    }

Be sure to get some kind of event (in my example a mouse event). Make sure they are enabled.

[window setAcceptsMouseMovedEvents:YES];

Upvotes: 1

Related Questions