Skip to content

Instantly share code, notes, and snippets.

@phistuck
Last active April 8, 2023 12:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phistuck/fbfa9896be0ae83f2bf9661a9f462621 to your computer and use it in GitHub Desktop.
Save phistuck/fbfa9896be0ae83f2bf9661a9f462621 to your computer and use it in GitHub Desktop.
A proof of concept to make sure that an NSButton with setKeyEquivalent:@"\r" next to an NSTableView responds to pressing Enter/Return when the table has the focus
// To compile, add AppKit.Framework to the required frameworks for the project
// Includes and modifies code from -
// https://github.com/eonil/CocoaProgrammaticHowtoCollection/tree/master/ComponentUsagesInObjectiveC/FileDropIntoTableView
// Created by Hoon H. on 2014/06/13.
// Copyright (c) 2014 Eonil. All rights reserved.
// https://source.chromium.org/chromium/chromium/src/+/main:chrome/browser/ui/cocoa/task_manager_mac.h
// https://source.chromium.org/chromium/chromium/src/+/main:chrome/browser/ui/cocoa/task_manager_mac.mm
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found at https://source.chromium.org/chromium/chromium/src/+/main:LICENSE;drc=48340c1e35efad5fb0253025dcc36b3a9573e258;l=0
#import <Cocoa/Cocoa.h>
@interface TestWindowController: NSWindowController <NSTableViewDataSource>
@end
@implementation TestWindowController {
NSWindow *window;
NSTableView *tableView;
NSButton *_endProcessButton;
NSScrollView *scrollView;
}
- (instancetype)initWithWindow:(NSWindow *)window {
self = [super initWithWindow:window];
window = [[NSWindow alloc] init];
window.delegate = self;
[window setContentSize:CGSizeMake(350, 200)];
[window makeKeyAndOrderFront:self];
NSTableColumn *c1 = [[NSTableColumn alloc] init];
c1.headerCell.stringValue = @"Task";
tableView = [[NSTableView alloc] init];
tableView.delegate = self;
[tableView addTableColumn:c1];
_endProcessButton =
[NSButton buttonWithTitle:@"Kill"
target:self
action:@selector(killSelectedProcesses:)];
[_endProcessButton setKeyEquivalent:@"\r"];
scrollView = [[NSScrollView alloc] initWithFrame:NSMakeRect(10, 50, 300, 150)];
NSView *contentView = window.contentView;
scrollView.documentView = tableView;
[contentView addSubview:scrollView];
[contentView addSubview:_endProcessButton];
tableView.dataSource = self;
return self;
}
- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)rowIndex {
[tableColumn setEditable:NO];
return [NSString stringWithFormat:@"Tab %d", rowIndex];
}
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
return 5;
}
- (IBAction)killSelectedProcesses:(id)sender {
NSString *s = [NSString stringWithFormat:@"%d", tableView.selectedRow];
[[NSAlert alertWithMessageText:s defaultButton:s alternateButton:@"" otherButton:@"" informativeTextWithFormat:@""] runModal];
}
@end
@interface BoilerplateApplicationController: NSResponder <NSApplicationDelegate>
@end
@implementation BoilerplateApplicationController { TestWindowController *windowController; }
- (void)applicationDidFinishLaunching:(NSNotification *)notification { windowController = [[TestWindowController alloc] init]; }
@end
int main(int argc, const char *argv[]) {
@autoreleasepool {
BoilerplateApplicationController *controllerDelegate = [[BoilerplateApplicationController alloc] init];
NSApplication *application = [NSApplication sharedApplication];
application.delegate = controllerDelegate;
[application run];
}
}
@phistuck
Copy link
Author

phistuck commented Apr 8, 2023

proof-of-concept-table-view-set-key-equivalent

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment