3

in my Mac application, I have a NSTableView. The default behavior is that clicking on the spacebar on the keyboard it selects a row and event tableView shouldSelectRow.

In my use case however I have a button that starts and stops audio playback. I would like to remove the spacebar handling from the NSTableView and move it to the playback button.

I tried to introduce a menu item for playback and assigned the spacebar key to it. This does not work however. Every time I click the spacebar the NSTableView fires and not the menu entry.

My question is: How can I remove the spacebar listener from the NSTableView ?

thanks in advance.

4
  • Have you tried assigning the Space key as the key equivalent for the button? You can do this in the NIB. Select the button. Switch to the Attributes inspector. Click on the Key Equivalent field. Press the Space key. Jan 15, 2015 at 2:05
  • Hi @KenThomases, this is actually a good idea and it seems to work however, the NSTableView still fires shouldSelectRow. I need to somehow remove the space triggering the shouldSelectRow event. Any ideas ?
    – Mozzak
    Jan 15, 2015 at 2:35
  • Log the result of [NSThread callStackSymbols] from the delegate method and edit your question to include it. Jan 15, 2015 at 2:49
  • Hi @Ken, I think I found a way to do it. I overwrite NSTableView with a custom class and implement keyDown. Now I can reroute Space Key event and leave all other key events for the NSTableView in tact. That works quite nice.
    – Mozzak
    Jan 15, 2015 at 3:02

2 Answers 2

8

It can be done using the NSTableViewDelegate protocol, avoids having to subclass NSTableView:

- (BOOL)tableView:(NSTableView *)tableView shouldTypeSelectForEvent:(NSEvent *)event withCurrentSearchString:(NSString *)searchString {
    return ([event.charactersIgnoringModifiers characterAtIndex:0]!=0x20);
}
1

I found a solution to the problem outlined.

I overwrite my NSTableView and implemented keyDown event where I was able to intercept the spacebar key event.

@implementation AudioFileTableView

- (void)drawRect:(NSRect)dirtyRect {
    [super drawRect:dirtyRect];

    // Drawing code here.
}

-(void)keyDown:(NSEvent *)theEvent{

    unichar key = [[theEvent charactersIgnoringModifiers] characterAtIndex:0];

    if(key == 0x0020)
    {
        // handle the spacebar event
        return;
    }

    [super keyDown:theEvent];
}

@end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.