External Bluetooth Keyboard integration in IOS 7

て烟熏妆下的殇ゞ 提交于 2019-12-03 13:17:02

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!