How can I respond to external keyboard arrow keys?

醉酒当歌 提交于 2019-11-28 17:19:59

For those who are looking for a solution under iOS 7 - there is a new UIResponder property called keyCommands. Create a subclass of UITextView and implement keyCommands as follows...

@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
{

}

Sorted! I simply use a 1x1px text view and use the delegate method textViewDidChangeSelection:

EDIT: For iOS 6 I had to change the text view to 50x50px (or at least enough to actually display text) for this to work

I also managed to suppress the on-screen keyboard when the pedal is disconnected.

This is my code in 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];

keyboardShown is declared as a bool in my header file.

Then add these methods:

- (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;
}

I hope this code helps someone else who is looking for a solution to this problem. Feel free to use it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!