gpt4 book ai didi

macos - 如何检测 NSTableView 上的 rightMouseDown 和 **otherMouseDown** ?

转载 作者:行者123 更新时间:2023-12-03 17:38:03 24 4
gpt4 key购买 nike

我想检测 NSTableView 上的 rightMouseDown 和 otherMouseDown 。我搜索它并找到一些答案使用 menuForEvent 但是,当鼠标右键按下时它会调用,但我想检测 nstableview 上的鼠标单击。

最佳答案

有几种方法:

如果您子类化 NSTableView,则可以重写 NSResponder rightMouseDown:otherMouseDown: 函数。

如果您不想子类化NSTableView,您可以附加一个本地监视器。问题是这个本地监视器将查看应用程序中指定的每个事件,因此您必须进行一些检查以确保当发生右键或其他鼠标按下事件时鼠标位于 TableView 内。

[NSEvent addLocalMonitorForEventsMatchingMask:NSEventMaskRightMouseDown|NSEventMaskOtherMouseDown handler:^NSEvent * _Nullable(NSEvent * _Nonnull theEvent) {
if ([theEvent window] == [tableView window]) {
NSPoint event_location = [theEvent locationInWindow];
NSPoint local_point = [[tableView superview] convertPoint:event_location fromView:nil];

if (NSPointInRect(local_point, [tableView frame])) {

}
}

return theEvent;
}];

请注意,此方法返回一个 id 对象,您需要在必要时将其传递给 removeMonitor(就像 NSNotificationCenter addObserverForName:)。

关于macos - 如何检测 NSTableView 上的 rightMouseDown 和 **otherMouseDown** ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38175679/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com