1

I have an NSTableView that is not responding to up/down arrows keys. From my understanding responding IS the default behaviour, so I'm not sure what is going on (Note, I am NOT overriding any keyboard commands). The only setup I do for my table is:

[tableView setDoubleAction: @selector(doubleClickedRow)];
[tableView setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
[tableView registerForDraggedTypes: [NSArray arrayWithObjects: NSFilenamesPboardType, nil]];
[tableView setDraggingSourceOperationMask: NSDragOperationCopy forLocal:NO];

But from what I can tell, none of these would cause any problems.

Does anyone have any suggestions on where I could look for the root of this problem?

Kyle
  • 17,317
  • 32
  • 140
  • 246
  • What's value of `refusesFirstResponder` set in nib? – hamstergene Oct 28 '11 at 11:29
  • Can you click a row to select it? Does it become highlighted? In my table views, that's the point at which I can start using up- and down-arrows. – paulmelnikow Oct 29 '11 at 23:00
  • Yeah, I can click on the row to select it. Then once I try hitting the error keys, nothing happens. I'll check the value of `refusesFirstResponder` tonight. – Kyle Oct 31 '11 at 12:56

5 Answers5

3

An NSTableView will also be unresponsive to arrow keys if the NSWindow that contains the table is not the key window.

A situation where this can occur which I recently ran into is if the NSWindow's "Title Bar" appearance option is not set in Interface Builder: that changes the default behaviour of NSWindow to refuse key status. See -[NSWindow canBecomeKeyWindow].

Thus even where the NSWindow is presented as a modal sheet without a title bar, the Title Bar option must be set or key events won't reach the NSTableView.

spinacher
  • 549
  • 6
  • 10
1

Did you override keyDown: anywhere? Either in a custom NSTableView sub-class or maybe in the window controller? This could be the cause whey the arrow keys don't work in your table.

I ran into the same problem with a custom table view. I solved it by calling super for the arrow keys to let the table handle the event:

@implementation ILCustomTableView

- (void)keyDown:(NSEvent *)theEvent 
{
    if ([theEvent modifierFlags] & NSNumericPadKeyMask)
    {
        [super keyDown:theEvent];    // Let table view handle arrow keys
    } else {

        [self interpretKeyEvents:[NSArray arrayWithObject:theEvent]];        
    }
}

- (void)insertNewline:(id)sender
{
    // Enter key. Do special stuff here...
}
@end
Mark
  • 6,647
  • 1
  • 45
  • 88
0

Just ran into this problem, and I found this line prevents the up/down arrow keys from selecting rows correctly.

[self.tableView setSelectionHighlightStyle:NSTableViewSelectionHighlightStyleNone];

I was trying to use my own background color so I added that. And when I tapped on up/down, it triggered shouldSelectRow:, but it was always 0! Commenting out the above line fixed it for me.

Erik Villegas
  • 954
  • 13
  • 29
0

I had the same problem, that with the arrow keys my tableview's delegate didn't get notified of the change.

This was my Swift solution:

First sub-classing NSTableView and then overriding this method.

override func keyDown(theEvent: NSEvent) {
    super.keyDown(theEvent)
    let code=theEvent.keyCode
    if code >= 123 && theEvent.keyCode <= 126 {
        if let target=target{
            target.performSelector(action)
        }
    }
}

The 123 to 126 codes are Left, Right, Down and Up respectively

omarzl
  • 589
  • 4
  • 9
0

I have an NSTableView that is not responding to up/down arrows keys. From my understanding this is default behaviour

This is not default behavior. NSTableView does handle keyboard events and allows navigation with the cursor keys.

Maybe you can debug the responder chain with the trick in the following question: How to inspect the responder chain?

Community
  • 1
  • 1
Thomas Zoechling
  • 34,177
  • 3
  • 81
  • 112
  • Huh? "This is not default behavior" seems to contradict "NSTableView does handle keyboard events." – paulmelnikow Oct 29 '11 at 00:45
  • If I understand the question correctly, Zenox assumes that NSTableView does not handle keyboard events. This assumption is wrong. NSTableView's default implementation provides keyboard navigation. – Thomas Zoechling Oct 29 '11 at 07:26