Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
321 views
in Technique[技术] by (71.8m points)

ios - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
{

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...