Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: How to detect the escape/control keys on a hardware bluetooth keyboard?

Tags:

I'm trying to figure out how to detect that the Escape (and other key combinations like Ctrl and alt) have been pressed on a bluetooth keyboard attached to an iOS device.

Some answers seem to suggest this isn't possible. However there are apps in the Appstore that do this (for example iSSH) so I assume it is possible using the public APIs somehow.

I've tried creating my own UITextInput however this receives nothing when the Escape key is pressed. The only part of the API I can see where the iPad might respond is when VoiceOver is enabled (Escape works as back in Safari), so I'm wondering if there's a way in via the accessibility API?

I've also tried to see if there's something I can observe from NSNotificationCenter that might help, but have yet to find anything.

Suggestions welcome, I've been hacking away at this for a day and I'm at a bit of a loss now.

like image 486
new299 Avatar asked Feb 09 '13 20:02

new299


People also ask

Where is the Esc button on iPad keyboard?

The iPad Magic Keyboard and Smart Keyboard do not have physical ESC / escape keys, and while there are various options to type Escape on the iPad, nothing really emulates a physical hardware ESC key if you need one for something like vim.

How do you press Escape on keyboard?

Short for Escape, Esc is a key found on the top-left corner of a computer keyboard. It allows the user to abort, cancel, or close an operation.


Video Answer


2 Answers

You can do this now in iOS 7. For example, to implement the escape key, override UITextView and place the following methods in your class:

- (NSArray *) keyCommands {     UIKeyCommand *esc = [UIKeyCommand keyCommandWithInput: UIKeyInputEscape modifierFlags: 0 action: @selector(esc:)];     return [[NSArray alloc] initWithObjects: esc, nil]; }  - (void) esc: (UIKeyCommand *) keyCommand {     // Your custom code goes here. } 

You don't need to check to be sure you are on iOS 7, since earlier versions of the OS won't call the keyCommands method.

like image 163
Mike Avatar answered Sep 19 '22 12:09

Mike


There are no public APIs for what you intend to accomplish, so this may lead to a rejection.

If you are willing to risk it, you can try this. Wich basically intercepts all events sent to your App by overwriting sendEvent: in your UIApplication.

like image 40
Daniel Avatar answered Sep 20 '22 12:09

Daniel