问题
I need to support external keyboard functionality in my app and need key combinations like Alt+Tab Tab to be detected in the app to trigger some event. In IOS 6 I had overridden the
- (void)sendEvent:(UIEvent *)anEvent;
function in the UIApplication subclass to get the key pressed combinations on external keyboard.
But now I am testing my app in IOS 7 and the sendEvent doesn't even seem to get called for any hardware key pressed event.
Any Solutions..?
回答1:
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.
回答2:
One the ways to detect hardware keyboard button pressed events in IOS7.x can be to override the method -(void)handleKeyUIEvent:(id)anEvent in the UIApplication.
But this is a private method for ios, may lead to appstore rejections.
来源:https://stackoverflow.com/questions/19152871/external-bluetooth-keyboard-integration-in-ios-7