You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让NSOpenPanel支持⌘A(全选项目)快捷键绑定?

NSOpenPanel无法使用⌘A全选文件的问题

MacVim是一款macOS应用,其文件打开对话框会调用NSOpenPanel并通过一个简单视图显示隐藏文件(可查看MMAppController.m的fileOpen方法和Miscellaneous.m的showHiddenFilesView方法)。

使用MacVim打开文件时,我可以使用⌘A快捷键全选文件。但我看不出它的实现和我下方的代码有何不同,而我的代码无法使用⌘A全选文件。

NSOpenPanel *panel = [NSOpenPanel openPanel];
[panel setCanChooseFiles:YES];
[panel setCanChooseDirectories:YES];
[panel setAllowsMultipleSelection:YES];
if ([panel runModal] == NSModalResponseOK) {
    for ( NSURL* URL in [panel URLs] ) {
        NSLog( @"%@", [URL path] );
    }
} else {
    NSLog( @"ok button not pressed");
}

NSOpenPanel及其父类NSSavePanel的文档中,并未提及可设置的快捷键绑定或其他布尔值。我唯一看到可能有用的是自定义视图,但MacVim似乎并未进行相关的NSView操作。

以下是完整的MCV示例及其编译方式。我怀疑示例存在问题,因为必须先点击图形矩形才能让打开面板正常工作,但我认为这不会影响快捷键绑定。

#import <Cocoa/Cocoa.h>

void openDialog () {
    @try {
        NSOpenPanel *panel = [NSOpenPanel openPanel];
        [panel setCanChooseFiles:YES];
        [panel setCanChooseDirectories:YES];
        [panel setAllowsMultipleSelection:YES];
        if ([panel runModal] == NSModalResponseOK) {
            for ( NSURL* URL in [panel URLs] ) {
                NSLog( @"%@", [URL path] );
            }
        } else {
            NSLog( @"ok button not pressed");
        }
    } @catch (NSException *exception) {
        NSLog(@"%@", [exception callStackSymbols]);
    }
}

void setup () {
    NSWindow *myWindow;
    NSRect    graphicsRect = NSMakeRect(100.0, 350.0, 400.0, 400.0);

    myWindow = [ [NSWindow alloc]
               initWithContentRect: graphicsRect
                         styleMask:NSWindowStyleMaskTitled
                                  |NSWindowStyleMaskClosable
                                  |NSWindowStyleMaskMiniaturizable
                           backing:NSBackingStoreBuffered
                             defer:NO ];

    [myWindow setTitle:@"Open File App"];
    [myWindow makeKeyAndOrderFront: nil];
    openDialog();
}

int main (  ) {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSApp = [NSApplication sharedApplication];
    setup();
    [NSApp run];
    [NSApp release];
    [pool release];
    return(EXIT_SUCCESS);
}

编译命令:

gcc -o $@ -framework Cocoa -isysroot $(SDK) -fobjc-exceptions -std=c99 ofdtest.m

补充信息

  • macOS版本:Sonoma 14.2.1
  • Xcode版本:15.2(15C500b)
  • 使用的SDK路径:/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk
  • 运行环境:在iTerm2中运行编译后的MCV

内容的提问来源于stack exchange,提问作者Jeff Holt

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 23:00:30