Skip to content

Instantly share code, notes, and snippets.

Last active April 8, 2023 12:55
Show Gist options
  • 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 -
// Created by Hoon H. on 2014/06/13.
// Copyright (c) 2014 Eonil. All rights reserved.
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found at;drc=48340c1e35efad5fb0253025dcc36b3a9573e258;l=0
#import <Cocoa/Cocoa.h>
@interface TestWindowController: NSWindowController <NSTableViewDataSource>
@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"
[_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];
@interface BoilerplateApplicationController: NSResponder <NSApplicationDelegate>
@implementation BoilerplateApplicationController { TestWindowController *windowController; }
- (void)applicationDidFinishLaunching:(NSNotification *)notification { windowController = [[TestWindowController alloc] init]; }
int main(int argc, const char *argv[]) {
@autoreleasepool {
BoilerplateApplicationController *controllerDelegate = [[BoilerplateApplicationController alloc] init];
NSApplication *application = [NSApplication sharedApplication];
application.delegate = controllerDelegate;
[application run];
Copy link

phistuck commented Apr 8, 2023


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