Swift: NSStatusItem menu behaviour in 10.10 (e.g. show only on right mouse click)

前端 未结 2 2163
夕颜
夕颜 2021-02-14 22:36

I am writing a simple status bar app in Swift, and attempting to use the new NSStatusItem API introduced in OS X 10.10.

The interface I\'m aiming for is a simple left mo

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-14 22:52

    This is deprecated in 10.10, but will continue to work:

    [self.statusItem setTarget:self]; // Otherwise this goes to the first responder
    [self.statusItem setAction:@selector(statusItemClicked:)];
    [self.statusItem sendActionOn:(NSRightMouseUpMask)];
    

    You can set other events in setActionOn by bitmasking them. So for instance if you wanted left and right click:

    [self.statusItem sendActionOn:(NSLeftMouseUpMask | NSRightMouseUpMask)];
    

    (Excuse the objC, you should be able to translate it into swift and it should work)

提交回复
热议问题