开发者

NSStatusItem (cocoa) location on screen

I am trying to get the on screen location of an NSStatusItem so that I can pe开发者_如何学运维rform a click on that area via code like below. I am doing this so that my users can press a hotkey to see the menu.

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

Does anyone know of a way to get the location?, I have been trying ideas and searching for days and have found several ideas but none of them seem to work in leopard/snow leopard

The NSStatusItem is using an NSMenu not a custom view.


Don't do this by simulating a mouse event, that is completely the wrong way to go.

You can use the -popUpStatusItemMenu: method of NSStatusItem to show the menu. You can call this method from a hot key by using a CGEventTap to capture global events that your NSStatusItem can respond to.

On 10.6 you can use the +addGlobalMonitorForEventsMatchingMask:handler: method of NSEvent to capture global events instead of using CGEventTap directly.


Thinking out loud you could create your own custom view for the NSStatusItem. Then for example from the 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]];

And to simulate the highlight you'd send a message to your custom view. Then when drawing MyCustomView:

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

As well as using -popUpStatusItemMenu:.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜