Location of NSStatusItem (cocoa) on the screen

I am trying to get the location on the NSStatusItem screen so that I can click on this area using the code as shown below. I do this so that my users can press the hotkey to see the menu.

event = CGEventCreateMouseEvent(NULL, kCGEventLeftMouseDown, newLocation, kCGMouseButtonLeft);
CGEventPost(kCGHIDEventTap, event);
CFRelease(event);

Does anyone know how to get a seat? I tried to try ideas and look for days and found some ideas, but none of them seem to work in a leopard / snow leopard

NSStatusItem uses NSMenu, not a custom view.

+3
source share
2 answers

Do not do this while simulating a mouse event; this is a completely wrong path.

-popUpStatusItemMenu: NSStatusItem, . , CGEventTap , NSStatusItem.

10.6 +addGlobalMonitorForEventsMatchingMask:handler: NSEvent CGEventTap.

+3

, NSStatusItem. , , AppController:

- (void)awakeFromNib {    
float width = 30.0;
float height = [[NSStatusBar systemStatusBar] thickness];
NSRect viewFrame = NSMakeRect(0, 0, width, height);
statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:width] retain];
[statusItem setView:[[[MyCustomView alloc] initWithFrame:viewFrame controller:self] autorelease]];

, . MyCustomView:

// In MyCustomView.m
- (void)drawRect:(NSRect)rect {
if (clicked) {
    [[NSColor selectedMenuItemColor] set];
    NSRectFill(rect);
}

, -popUpStatusItemMenu:.

0

Source: https://habr.com/ru/post/1722380/


All Articles