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..?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With