Objective-C: NSStatusBar right and left click

How to detect a left or right click on the status bar icon, and not take any action, depending on which mouse button (trackpad) was pressed?

I use:

_statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength] retain]; [_statusItem setMenu:menu]; [_statusItem setHighlightMode:YES]; 

To create a status bar icon

+6
objective-c nsstatusitem
source share
2 answers

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

To detect a click on the status bar icon, you may detect a menu that appears. Before opening the menu, he sends a message to menuWillOpen: to his delegate (if any). So, implement something like this:

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

You also need to set a delegate for the menu, e.g.

 [menu setDelegate:self]; 
+13
source share

I found another solution, see here.

Cocoa: right click NSStatusItem

+3
source share

All Articles