在Mac OSX中双击菜单栏图标的操作 [英] Double-click action for menu bar icon in Mac OSX

查看:169
本文介绍了在Mac OSX中双击菜单栏图标的操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个显示菜单栏图标的Mac OSX小型应用程序。单击后,将弹出一个菜单。

I'm writing a small Mac OSX app that displays a menu bar icon. When clicked, a menu pops up.

我希望菜单栏图标具有默认操作。基本上,双击时执行一个动作,而不必从菜单中选择该动作。

I'd like to have a "default" action for the menu bar icon. Basically, to execute an action when double-clicking it, without having to select the action from the menu.

我查看了Apple文档,并且里面有这样的东西 NSStatusItem 称为 doubleAction ,但是它已被软淘汰,并且(似乎)不起作用。此外,它说使用 button 属性的文档,但尝试这样做会导致如下所示的编译器错误:

I looked over the Apple docs and there's is such a thing in NSStatusItem called doubleAction, but it's soft deprecated and does not (seem to) work. More over, the docs it says to use the button property, but trying to do so results in the compiler error shown below:

非常感谢任何代码或指南,谢谢!

Any code or guidance are much appreciated, thanks!

推荐答案

今天的情况(Xcode 7.3.1,OSX 10.11.4):

The situation as it stands today (Xcode 7.3.1, OSX 10.11.4):


  • NSStatusItem doubleAction 已被弃用(并且不起作用)。

  • Apple告诉您使用按钮属性-但是没有 doubleAction 的标题(我想知道(如果实现存在)。哦,它也是只读的。

  • 在任何 NSStatusItem '中,没有其他关于左/右/双击的选项。

  • the doubleAction of NSStatusItem is deprecated (and NOT actually working).
  • Apple tells you to use the button property - but there's no header for doubleAction (I wonder if the implementation exists). Oh, it's also read-only.
  • there are no other options regarding left/right/double click in any of the NSStatusItem's properties.

解决方法:为NSButton创建一个类别(与Apple所说的完全相同) )并实现自定义点击处理程序,该处理程序会在检测到双击时发布通知,如下所示:

The workaround: create a category for NSButton (the exact same that Apple was talking about) and implement a custom click handler that posts a notification when a double click was detected, like the following:

@implementation NSButton (CustomClick)

- (void)mouseDown:(NSEvent *)event {
    if (self.tag != kActivateCustomClick) {
        [super mouseDown:event];
        return;
    }

    switch (event.clickCount) {
        case 1: {
            [self performSelector:@selector(callMouseDownSuper:) withObject:event afterDelay:[NSEvent doubleClickInterval]];
            break;
        }
        case 2: {
            [NSRunLoop cancelPreviousPerformRequestsWithTarget:self];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"double_click_event" object:nil];
            break;
        }
    }
}

- (void)callMouseDownSuper:(NSEvent *)event {
    [super mouseDown:event];
}

@end

如您所见,处理程序仅处理具有特定标签值的 NSButton 实例。

As you can see, this handler only handles NSButton instances that have a specific tag value.

当检测到单击时,我将呼叫推迟到 super 以便按系统的双击间隔进行处理。如果在这段时间内我再次点击,我取消了对 super 的呼叫并将其视为双击。

When a click is detected, I defer the call to super for handling by the system's double-click interval. If within that time I receive another click, I cancel the call to super and treat it as a double-click.

希望有帮助!

这篇关于在Mac OSX中双击菜单栏图标的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆