2011-11-02 11 views
11

私はこれまでに尋ねられていることを知っています。私が見た唯一の答えは、「UIガイドラインに反するので、外付けキーボードを必要としません」です。しかし、私はhttp://www.bilila.com/page_turner_for_ipadのようなフットペダルを使って、スワイプに加えてアプリ内のページを変更したいと考えています。このページターナーは、キーボードをエミュレートし、上下の矢印キーを使用します。外部キーボードの矢印キーにどのように応答できますか?

ここに私の質問があります:これらの矢印キーイベントにどのように対応しますか?他のアプリが管理するようにする必要がありますが、私は空白を描いています。

答えて

21

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 
{ 

} 
+0

'UIWebView'でも使えますか? – Dmitry

+0

@Altaveron keyCommandsプロパティはUIResponderのプロパティであり、UIWebViewはUIResponderを継承しているため、うまくいかない理由はありません。 – amergin

+0

'UIWebView'は多くのサブビューのコンテナです。 – Dmitry

9

ソート済み!この仕事をする

のためのiOS 6の場合は、私は50x50px(あるいは少なくとも、実際にテキストを表示するのに十分な)テキストビューを変更しなければならなかった:私は単にEDIT textViewDidChangeSelection:

を1x1pxテキストビューを使用すると、デリゲートメソッドを使用します

また、ペダルを外したときにオンスクリーンキーボードを押さえることもできました。

これは、の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は私のヘッダファイルでboolとして宣言されています。

そして、これらのメソッドを追加します。

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

私はこのコードは、この問題に対する解決策を探している他の誰かに役立ちます願っています。それを自由に使用してください。

+1

は私のために素晴らしい仕事が、私はviewDidAppearないのviewDidLoadに初期化コードを入れていました。 – Bryan

+2

これは、無限ループにつながると思います。 'selectedRange'プロパティ(' textViewDidChangeSelection')を設定すると、別の 'textViewDidChangeSelection'がトリガされるからです。 –

+0

'selectedRange'はカーソルの位置が0または2の場合にのみ設定されるので、無限ループは発生しません。 – colincameron

関連する問題