Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I respond to external keyboard arrow keys?

I know this has been asked before, and the only answers I've seen are "Don't require an external keyboard, as it goes against UI guidelines". However, I want to use a foot pedal like this: http://www.bilila.com/page_turner_for_ipad to change between pages in my app (in addition to swiping). This page turner emulates a keyboard and uses the up/down arrow keys.

So here is my question: how do I respond to these arrow key events? It must be possible as other apps manage, but I'm drawing a blank.

like image 679
colincameron Avatar asked Nov 02 '11 12:11

colincameron


1 Answers

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
{

}
like image 142
amergin Avatar answered Sep 22 '22 13:09

amergin