Search code examples
ioskeyboardarrow-keys

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.


Solution

  • 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
    {
    
    }