7

我需要在我的应用程序中支持外部键盘功能,并且需要在应用程序中检测到 Alt+Tab Tab 等组合键来触发某些事件。在 IOS 6 中,我覆盖了

- (void)sendEvent:(UIEvent *)anEvent;

子类中的函数UIApplication以获取外部键盘上的按键组合。

但是现在我正在 IOS 7 中测试我的应用程序,并且 sendEvent 似乎甚至没有被任何硬件按键事件调用。

任何解决方案..?

4

2 回答 2

18

There is a 100% supported way to handle keyboard shortcuts on a Bluetooth keyboard in iOS 7 using the new UIKeyCommand class and the UIResponder chain. I did blog about this, but here's the gist:

Somewhere in your Responder chain add a method for keyCommands that returns an array of UIKeyCommand objects:

- (NSArray *)keyCommands {
    UIKeyCommand *commandF = [UIKeyCommand keyCommandWithInput:@"f" modifierFlags:UIKeyModifierCommand action:@selector(handleCommandF:)];
    return @[commandF];
}

Then, when ⌘F is pressed (in a text input view) the Responder chain will look for that handleCommandF method. If there are multiple definitions, it'll use the most tightly scoped one (eg. the View itself takes precedence over a ViewController).

Do note that this will only work when an input (such as UITextField or UITextView) is the first responder. If you want 'global' shortcuts in your app you can do the trick where you hide a UITextField offscreen and focus on that.

于 2013-10-11T23:10:53.493 回答
-1

在 IOS7.x 中检测硬件键盘按钮按下事件的一种方法是覆盖 UIApplication 中的方法 -(void)handleKeyUIEvent:(id)anEvent。

但这是 ios 的私有方法,可能会导致应用商店拒绝。

于 2013-10-09T09:19:18.730 回答