是否可以在 OS X 中处理菜单栏上的点击事件?
Is it possible to handle click event on menu bar in OS X?
是否可以在 OS X 中处理菜单栏上的点击事件?
我是说白色 space,不是菜单项。
我试过了 - Detect click on OS X menu bar? - addLocalMonitorForEventsMatchingMask
[NSEvent addLocalMonitorForEventsMatchingMask: (NSLeftMouseDownMask | NSRightMouseDownMask | NSOtherMouseDownMask | NSKeyDownMask) handler:^(NSEvent *incomingEvent) {
NSEvent *result = incomingEvent;
NSWindow *targetWindowForEvent = [incomingEvent window];
return result;
}];
我也尝试了 addGlobalMonitorForEventsMatchingMask。
没有任何结果。可能吗?
谢谢。
您可以使用事件点击来全局跟踪鼠标事件,然后只计算点击是否在您想要的矩形内:
#define kMenuBarHeight 22.0
CGEventRef leftMouseTapCallback(CGEventTapProxy aProxy, CGEventType aType, CGEventRef aEvent, void* aRefcon)
{
CGPoint theLocation = CGEventGetLocation(aEvent);
if (theLocation.y <= kMenuBarHeight) {
NSLog(@"CLICKED THE MENUBAR");
}
return aEvent;
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
CGEventMask mask = CGEventMaskBit(kCGEventLeftMouseDown) | CGEventMaskBit(kCGEventLeftMouseUp);
CFMachPortRef leftMouseEventTap = CGEventTapCreate(kCGSessionEventTap, kCGHeadInsertEventTap, 0, mask, leftMouseTapCallback, NULL);
if (leftMouseEventTap) {
CFRunLoopSourceRef runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, leftMouseEventTap, 0);
CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopCommonModes);
CGEventTapEnable(leftMouseEventTap, true);
}
}
是否可以在 OS X 中处理菜单栏上的点击事件?
我是说白色 space,不是菜单项。
我试过了 - Detect click on OS X menu bar? - addLocalMonitorForEventsMatchingMask
[NSEvent addLocalMonitorForEventsMatchingMask: (NSLeftMouseDownMask | NSRightMouseDownMask | NSOtherMouseDownMask | NSKeyDownMask) handler:^(NSEvent *incomingEvent) {
NSEvent *result = incomingEvent;
NSWindow *targetWindowForEvent = [incomingEvent window];
return result;
}];
我也尝试了 addGlobalMonitorForEventsMatchingMask。
没有任何结果。可能吗?
谢谢。
您可以使用事件点击来全局跟踪鼠标事件,然后只计算点击是否在您想要的矩形内:
#define kMenuBarHeight 22.0
CGEventRef leftMouseTapCallback(CGEventTapProxy aProxy, CGEventType aType, CGEventRef aEvent, void* aRefcon)
{
CGPoint theLocation = CGEventGetLocation(aEvent);
if (theLocation.y <= kMenuBarHeight) {
NSLog(@"CLICKED THE MENUBAR");
}
return aEvent;
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
CGEventMask mask = CGEventMaskBit(kCGEventLeftMouseDown) | CGEventMaskBit(kCGEventLeftMouseUp);
CFMachPortRef leftMouseEventTap = CGEventTapCreate(kCGSessionEventTap, kCGHeadInsertEventTap, 0, mask, leftMouseTapCallback, NULL);
if (leftMouseEventTap) {
CFRunLoopSourceRef runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, leftMouseEventTap, 0);
CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopCommonModes);
CGEventTapEnable(leftMouseEventTap, true);
}
}