我有一个捕获 mouseDown 事件的 NSView。
我使用以下方法协调鼠标单击:
- (void)mouseDown:(NSEvent *)theEvent {
NSPoint touchPoint = [NSEvent mouseLocation];
//Rest of code
}
但是,这会将 clickPoint 设置为鼠标在全局屏幕坐标中的位置。
我想在相对 View 协调中获得点击的位置 - 如果用户在窗口的左下角点击,我希望点是 (0, 0) 无论包含窗口在屏幕上的哪个位置。
最佳答案
您应该使用 locationInWindow
,不是 mouseLocation
. locationInWindow
上的文档展示如何从那里出发:
NSPoint event_location = [theEvent locationInWindow];
NSPoint local_point = [self convertPoint:event_location fromView:nil];
关于macos - 在 View 坐标系中获取 [NSEvent 鼠标位置],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23787842/