在视图协调系统中获取[NSEvent mouseLocation]

Niv*_*Niv 12 macos nsevent mousedown

我有一个捕获mouseDown事件的NSView.

我正在使用以下方式协调鼠标点击:

- (void)mouseDown:(NSEvent *)theEvent {
    NSPoint touchPoint = [NSEvent mouseLocation];
    //Rest of code
}
Run Code Online (Sandbox Code Playgroud)

但是,这会将clickPoint设置为全局屏幕协调中鼠标的位置.

我想在相对视图协调中获得点击的位置 - 如果用户点击窗口的左下角,我想要的是点(0,0),无论屏幕上包含的窗口在哪里.

JWW*_*ker 24

你应该使用locationInWindow,而不是mouseLocation.文档locationInWindow展示了如何从那里开始:

NSPoint event_location = [theEvent locationInWindow];
NSPoint local_point = [self convertPoint:event_location fromView:nil];
Run Code Online (Sandbox Code Playgroud)