首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在NSTableView上检测NSTableView和**otherMouseDown**?

如何在NSTableView上检测NSTableView和**otherMouseDown**?
EN

Stack Overflow用户
提问于 2016-07-04 01:01:02
回答 1查看 364关注 0票数 0

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

EN

回答 1

Stack Overflow用户

发布于 2016-07-04 02:08:34

有几种方法:

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

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

代码语言:javascript
复制
[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:一样)。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38175679

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档