如何将键盘上/下事件从NSTextField转发到NSTableView? [英] How to forward keyboard up/down events from NSTextField to a NSTableView?

查看:113
本文介绍了如何将键盘上/下事件从NSTextField转发到NSTableView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试模拟Spotlight在优胜美地中的工作方式,其中当按下向上/向下箭头键并上下移动表格视图选择时,NSTextField(搜索字段)始终保持焦点.

I'm trying to simulate the way Spotlight works in Yosemite where the NSTextField (search field) always retains focus when hitting the up/down arrow keys and moves the tableview selection up and down.

我实现了以下代码:

- (BOOL)control:(NSControl *)control textView:(NSTextView *)textView doCommandBySelector:(SEL)commandSelector
{
    if (commandSelector == @selector(moveUp:)) {
        // move up
        return YES;
    } else if(commandSelector == @selector(moveDown:)){
        // move down
        return YES;
    }

    return NO;
}

虽然我可以使用它来上下移动行选择,例如:

While I could use this to then move the row selection up/down with something like:

[self.tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:currentRow ± 1] byExtendingSelection:NO];

我遇到的问题是,我已经创建了不应选择的节头"行,并且已经使用NSTableViewDelegate方法禁用了对这些行的选择:

The problem I have is that I've created Section Header rows that should not be selectable and I've disabled selection of these rows using the NSTableViewDelegate method:

- (BOOL)tableView: (NSTableView *)tableView shouldSelectRow: (NSInteger)row

但是发生的是,即使委托方法表明无法选择该行,selectRowIndexes:indexSetWithIndex:currentRowbyExtendingSelection:方法也会选择标题行.

But what happens is that the selectRowIndexes:indexSetWithIndex:currentRowbyExtendingSelection: method selects the header row even though the delegate method says that the row can't be selected.

似乎无论NSTableViewDelegate怎么说,您仍然可以以编程方式选择行.我想要的是跳过标题行的选择.

It seems you can still select rows programatically regardless of what the NSTableViewDelegate says. What I want is for the selection to jump the header rows.

如果NSTableView是firstResponder,则内置键盘控件会跳过标题行.

If the NSTableView is the firstResponder then the built-in keyboard controls do skip the header rows.

所以我的问题是是否有一种方法可以将向上/向下事件转发到NSTableView,从而使移动选择的内置机制起作用?

So my question is is there a way to forward the up/down events to the NSTableView so that the built in mechanism for moving the selection works?

推荐答案

以下内容对我有用,但是我不确定是否有任何副作用.

The following works for me, but I'm not sure if there are any side effects.

- (BOOL)control:(NSControl *)control textView:(NSTextView *)textView doCommandBySelector:(SEL)commandSelector
{
    if (commandSelector == @selector(moveUp:)) {
        // move up
        [_tableView keyDown:[NSApp currentEvent]];
        return YES;
    } else if(commandSelector == @selector(moveDown:)){
        // move down
        [_tableView keyDown:[NSApp currentEvent]];
        return YES;
    }

    return NO;
}

这篇关于如何将键盘上/下事件从NSTextField转发到NSTableView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆