如何响应外部键盘箭头键?

col*_*ron 11 keyboard arrow-keys ios

我知道之前已经问过,我见过的唯一答案是"不需要外部键盘,因为它违反了UI指南".但是,我想使用这样的脚踏板:http://www.bilila.com/page_turner_for_ipad来更改我的应用程序中的页面(除了滑动).此页面调整器模拟键盘并使用向上/向下箭头键.

所以这是我的问题:我如何回应这些箭头键事件?它必须可以像其他应用程序一样管理,但我正在画一个空白.

ame*_*gin 26

对于那些在iOS 7下寻找解决方案的人来说,有一个名为keyCommands的新UIResponder属性.创建UITextView的子类并实现keyCommands,如下所示......

@implementation ArrowKeyTextView

- (id) initWithFrame: (CGRect) frame
{
    self = [super initWithFrame:frame];
    if (self) {
    }
    return self;
}

- (NSArray *) keyCommands
{
    UIKeyCommand *upArrow = [UIKeyCommand keyCommandWithInput: UIKeyInputUpArrow modifierFlags: 0 action: @selector(upArrow:)];
    UIKeyCommand *downArrow = [UIKeyCommand keyCommandWithInput: UIKeyInputDownArrow modifierFlags: 0 action: @selector(downArrow:)];
    UIKeyCommand *leftArrow = [UIKeyCommand keyCommandWithInput: UIKeyInputLeftArrow modifierFlags: 0 action: @selector(leftArrow:)];
    UIKeyCommand *rightArrow = [UIKeyCommand keyCommandWithInput: UIKeyInputRightArrow modifierFlags: 0 action: @selector(rightArrow:)];
    return [[NSArray alloc] initWithObjects: upArrow, downArrow, leftArrow, rightArrow, nil];
}

- (void) upArrow: (UIKeyCommand *) keyCommand
{

}

- (void) downArrow: (UIKeyCommand *) keyCommand
{

}

- (void) leftArrow: (UIKeyCommand *) keyCommand
{

}

- (void) rightArrow: (UIKeyCommand *) keyCommand
{

}
Run Code Online (Sandbox Code Playgroud)


col*_*ron 9

排序!我只是使用1x1px文本视图并使用委托方法textViewDidChangeSelection:

编辑:对于iOS 6,我不得不将文本视图更改为50x50px(或至少足以实际显示文本)以使其工作

当踏板断开时,我还设法抑制了屏幕键盘.

这是我在viewDidLoad中的代码:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillAppear:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillDisappear:) name:UIKeyboardWillHideNotification object:nil];

UITextView *hiddenTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
[hiddenTextView setHidden:YES];
hiddenTextView.text = @"aa";
hiddenTextView.delegate = self;
hiddenTextView.selectedRange = NSMakeRange(1, 0);
[self.view addSubview:hiddenTextView];

[hiddenTextView becomeFirstResponder];
if (keyboardShown)
    [hiddenTextView resignFirstResponder];
Run Code Online (Sandbox Code Playgroud)

keyboardShownbool在我的头文件中声明为.

然后添加以下方法:

- (void)textViewDidChangeSelection:(UITextView *)textView {

    /******TEXT FIELD CARET CHANGED******/

    if (textView.selectedRange.location == 2) {

        // End of text - down arrow pressed
        textView.selectedRange = NSMakeRange(1, 0);

    } else if (textView.selectedRange.location == 0) {

        // Beginning of text - up arrow pressed
        textView.selectedRange = NSMakeRange(1, 0);

    }

    //  Check if text has changed and replace with original
    if (![textView.text isEqualToString:@"aa"])
        textView.text = @"aa";
}

- (void)keyboardWillAppear:(NSNotification *)aNotification {
    keyboardShown = YES;
}

- (void)keyboardWillDisappear:(NSNotification *)aNotification {
    keyboardShown = NO;
}
Run Code Online (Sandbox Code Playgroud)

我希望这段代码可以帮助那些正在寻找解决这个问题的人.随意使用它.

  • 我意识到这是旧的,但万一有其他人遇到它 - 有一个更好的方法来保持键盘隐藏在这样的输入视图上.只需添加一行:`textView.inputView = [[UIView alloc] initWithFrame:CGRectZero];` (3认同)
  • 我认为这会导致无限循环,因为设置`selectedRange`属性(在`textViewDidChangeSelection`中)会触发另一个`textViewDidChangeSelection`,尽管这个想法对我有帮助. (2认同)