Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

External Bluetooth Keyboard integration in IOS 7

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

like image 974
mmmanishs Avatar asked Oct 03 '13 07:10

mmmanishs


People also ask

How do I connect my keyboard to my iPhone 7?

Make sure the keyboard is turned on and charged. On iPhone, go to Settings > Bluetooth, then turn on Bluetooth. Select the device when it appears in the Other Devices list.

Can I use an external keyboard with my iPhone?

Control iPhone with an external keyboardGo to Settings > Accessibility > Keyboards, tap Full Keyboard Access, then turn on Full Keyboard Access. Control your iPhone using keyboard shortcuts. To customize the keyboard shortcuts, tap Commands.

Can I connect any Bluetooth keyboard to iPhone?

Thankfully, if needing to use an iPhone for extended typing or editing, any Bluetooth keyboard can be paired to bring a laptop-like experience to Apple's powerful smartphone.


1 Answers

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.

like image 149
swilliams Avatar answered Sep 22 '22 23:09

swilliams