开发者

Objective-C: NSStatusBar right and left click

开发者 https://www.devze.com 2022-12-14 03:06 出处:网络
how to detect left or right click on statusbar icon, and than make some action, depending on which mouse button (trackpad) was clicked?

how to detect left or right click on statusbar icon, and than make some action, depending on which mouse button (trackpad) was clicked?

I use:

  _statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength] retain];
  [_statusItem setMenu:menu];
  [_statusItem setHighlightMode:YE开发者_如何学运维S];

To generate statusBar icon


To detect the mouse buttons that are currently pressed, you can use [NSEvent pressedMouseButtons].

To detect the status bar icon being clicked, you can detect the menu appearing. Just before a menu opens, it sends a menuWillOpen: message to its delegate (if it has one). So implement something like this:

- (void)menuWillOpen:(NSMenu *)menu
{
  NSLog(@"%d",[NSEvent pressedMouseButtons]);
}

You will also need to set the delegate for the menu, for example by

[menu setDelegate:self];
0

精彩评论

暂无评论...
验证码 换一张
取 消