简体   繁体   English

检测右键单击上下文菜单osx

[英]Detect click on context menu osx

How can I make sure a method gets called each time I expand a NSMenu. 如何确保每次我扩展NSMenu时都会调用一个方法。 I have tried connecting the action from storyboard but the action only seem to get fired when i click menu items, not the menu itself. 我尝试从情节提要中连接动作,但是仅在单击菜单项时才触发动作,而不是菜单本身。

[item setAction: @selector(play:)]

I would like to run a method when for instance the help menu gets expanded, to update the enabled and disabled content of that menu, since it is supposed to be different for logged in and not logged in users.. 我想在例如扩展帮助菜单时运行一种方法,以更新该菜单的启用和禁用内容,因为对于登录用户和未登录用户来说,该方法应该有所不同。

Update: 更新:

I added the NSMenuDelegate protocol in brackets in my 我在我的括号中添加了NSMenuDelegate协议

 @interface ClientAppDelegate : NSObject<NSApplicationDelegate,NSMenuDelegate>

Adding menu items work but the menu does not seem to affect the delegate methods. 添加菜单项是可行的,但是菜单似乎并不影响委托方法。

// Create the application on the UI thread.
- (void)createApplication:(id)object {
  NSApplication* application = [NSApplication sharedApplication];
  [NSBundle loadNibNamed:@"MainMenu" owner:NSApp];

  // Set the delegate for application events.
  [application setDelegate:self];

  // Add the Tests menu.
  NSMenu* menubar = [application mainMenu];

  [menubar setDelegate:self];
    // TIDAL Create Controlls
    NSMenuItem *controlsItem = [[[NSMenuItem alloc] initWithTitle:@"Controls"
                                                           action:nil
                                                    keyEquivalent:@""] autorelease];
    NSMenu *controlsMenu = [[[NSMenu alloc] initWithTitle:@"Controls"] autorelease];
    AddMenuItem(controlsMenu, @"Pl",      ID_L_PL);

    [controlsItem setSubmenu:controlsMenu];
    [menubar addItem:controlsItem];


......


-(void) menuWillOpen:(NSMenu *)menu{
  wprintf(L"ITEM CLICK CAN I UPDATE MENU VISIBILITY HERE?");
}

-(void) menuNeedsUpdate:(NSMenu *)menu{
  wprintf(L"ITEM CLICK CAN I UPDATE MENU VISIBILITY HERE?");

}

Update2: 更新2:

So I started over, imaginging that the problem had something to do with references and the fact that i was developing in objective-c++. 因此,我重新开始,想象该问题与参考以及我正在使用Objective-C ++开发的事实有关。 However i can not get it to work in just a minimal Obj-C example either, below is my code. 但是我也无法在一个最小的Obj-C示例中使它工作,以下是我的代码。 The only callback that seems to work is the one that handles item clicks. 似乎唯一起作用的回调是处理项目单击的回调。 May the problem be present due to the fact that I am using : 可能由于我使用的事实而出现问题:

  _menubar = [application mainMenu];

  [_menubar setDelegate:self];

To get my menubar and setup my menu delegate? 要获取我的菜单栏并设置我的菜单代表?

//
//  AppDelegate.m
//  menuTest
//
//  Created by David Karlsson on 27/02/15.
//  Copyright (c) 2015 David Karlsson. All rights reserved.
//

#import "AppDelegate.h"

void AddMenuItem(NSMenu *menu, NSString* label, int idval) {
  NSMenuItem* item = [menu addItemWithTitle:label
                                     action:@selector(menuItemSelected:)
                              keyEquivalent:@""];
  [item setTag:idval];
}


@interface AppDelegate ()

@property (weak) IBOutlet NSWindow *window;
@property (strong) IBOutlet NSMenu * menubar;
@end

@implementation AppDelegate


- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
  NSLog(@"Launched menu test");
  // Insert code here to initialize your application
  NSApplication* application = [NSApplication sharedApplication];
  //[NSBundle loadNibNamed:@"MainMenu" owner:NSApp];

  // Set the delegate for application events.
  [application setDelegate:self];

  // Add the Tests menu.
  _menubar = [application mainMenu];

  [_menubar setDelegate:self];

  NSMenuItem *controlsItem = [[NSMenuItem alloc] initWithTitle:@"Controls"
                                                        action:nil
                                                 keyEquivalent:@""];
  NSMenu *controlsMenu = [[NSMenu alloc] initWithTitle:@"Controls"];
  AddMenuItem(controlsMenu, @"1",      123);
  AddMenuItem(controlsMenu, @"2",    124);
  AddMenuItem(controlsMenu, @"3",  154);

  [controlsItem setSubmenu:controlsMenu];
  [_menubar addItem:controlsItem];


}

-(void) menuWillOpen:(NSMenu *)menu{
  NSLog(@"ITEM CLICK CAN I UPDATE MENU VISIBILITY HERE?");
}

-(void) menuNeedsUpdate:(NSMenu *)menu{
  NSLog(@"ITEM CLICK CAN I UPDATE MENU VISIBILITY HERE?");

}




- (void)applicationWillTerminate:(NSNotification *)aNotification {
  // Insert code here to tear down your application
  NSLog(@"Teardown menu test");
}

- (IBAction)menuItemSelected:(id)sender {
  // Retrieve the active RootWindow.
  NSWindow* key_window = [[NSApplication sharedApplication] keyWindow];
  if (!key_window){
    return;
  }



  NSLog(@"CLICK");
}


@end

Set the menu's delegate, then use this object to implement menuNeedsUpdate: from the NSMenuDelegate protocol. 设置菜单的委托,然后使用该对象从NSMenuDelegate协议实现menuNeedsUpdate: NSMenuDelegate This method is called just before a menu is shown and is specifically provided to allow you to make changes to the menu in question before it arrives on screen. 此方法在显示菜单之前被调用,并且专门提供该方法,以使您可以在菜单出现在屏幕上之前对其进行更改。

您要实现NSMenu Delegate协议的某些部分,并且可能需要在菜单跟踪过程中工作的模式下运行的NSTimer,以便在菜单可见时进行实时更新。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用新服务项更新OSX右键上下文菜单 - Updating OSX right click context menu with new service item 你能在mouseDown事件中检测到ctrl-click(上下文菜单)请求吗? - Can you detect a ctrl-click (context menu) request in the mouseDown event? Mac OSx:将项目注入Finder的上下文菜单 - Mac OSx: Injecting item to Finder's Context Menu OSX / Cocoa:在菜单中为右键单击和服务列表设置服务 - OSX/Cocoa : setting up service in menu for right click and services list 在Mac OSX中双击菜单栏图标的操作 - Double-click action for menu bar icon in Mac OSX 在图像上单击菜单(或右键单击),然后在Mac OSX(Snow Leopard)中添加黑色边框 - Menu click (or right click) on a image and add black border in Mac OSX (Snow Leopard) 是否可以使用AppleScript在不访问“系统事件”的情况下单击osx菜单项? - Is there a way using AppleScript to click osx menu items without accessing “System Events”? 如何将上下文相关菜单添加到 NSOutlineView(即右键菜单) - How do you add context senstive menu to NSOutlineView (ie right click menu) 是否可以通过编程方式将项目添加到Mac OS的右键单击上下文菜单中? - Is it possible to add an item to the right-click context menu of a Mac OS programmatically? 在Webview中右键单击可可粉,重命名默认上下文菜单标题[Objective C] - Rename default context menu title on right click in webview for cocoa [Objective C]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM