How to Retrieve Keystrokes from a Custom Keyboard on an iOS App

How do I retrieve keystrokes from a custom keyboard on an iOS app?

Here's my custom keyboard which I believe addresses these as completely as Apple will allow:

//  PVKeyboard.h

#import <UIKit/UIKit.h>
@interface PVKeyboard : UIView
@property (nonatomic,assign) UITextField *textField;
@end

// PVKeyboard.m

#import "PVKeyboard.h"

@interface PVKeyboard () {
UITextField *_textField;
}
@property (nonatomic,assign) id<UITextInput> delegate;
@end

@implementation PVKeyboard

- (id<UITextInput>) delegate {
return _textField;
}

- (UITextField *)textField {
return _textField;
}

- (void)setTextField:(UITextField *)tf {
_textField = tf;
_textField.inputView = self;
}

- (IBAction)dataPress:(UIButton *)btn {
[self.delegate insertText:btn.titleLabel.text];
}

- (IBAction)backPress {
if ([self.delegate conformsToProtocol:@protocol(UITextInput)]) {
[self.delegate deleteBackward];
} else {
int nLen = [_textField.text length];
if (nLen)
_textField.text = [_textField.text substringToIndex:nLen-1];
}
}

- (IBAction)enterPress {
[_textField.delegate textFieldShouldReturn:_textField];
}

- (UIView *)loadWithNIB {
NSArray *aNib = [[NSBundle mainBundle]loadNibNamed:NSStringFromClass([self class]) owner:self options:nil];
UIView *view = [aNib objectAtIndex:0];
[self addSubview:view];
return view;
}

- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self)
[self loadWithNIB];
return self;
}
@end

In XCode 4.3 and later, you need to create an objective-Class (for the .h & .m files) based on UIView and a User Interface View file (for the .xib file). Make sure all three files have the same name. Using the Identity Inspector, make sure to set the XIB's File's Owner Custom Class to match the new object's name. Using the Attributes Inspector, set the form's size to Freeform and set the Status Bar to none. Using the Size Inspector, set the form's size, which should match the width of the standard keyboard (320 for iPhone portrait and 480 for iPhone landscape), but you can choose any height you like.

The form is ready to be used. Add buttons and connect them to the dataPress, backPress and enterPress as appropriate. The initWithFrame: and loadWithNIB functions will do all the magic to allow you to use a keyboard designed in Interface Builder.

To use this keyboard with a UITextField myTextField, just add the following code to your viewDidLoad:

self.keyboard = [[PVKeyboard alloc]initWithFrame:CGRectMake(0,488,320,60)];
self.keyboard.textField = self.myTextField;

Because of some limitations, this keyboard isn't reusable, so you'll need one per field. I can almost make it reusable, but I'm just not feeling that clever. The keyboard is also limited to UITextFields, but that's mainly because of limitations in implementing the enter key functionality, which I'll explain below.

Here's the magic that should allow you to design a better keyboard than this starter framework...

I've implemented the only property of this keyboard, textField, using a discreet a discrete setter (setTextField) because:

  1. we need the UITextField object to handle the enter problem
  2. we need UITextField because it conforms to the UITextInput protocol which conforms to UIKeyInput, which does much of our heavy lifting
  3. it was a convenient place to set the UITextInput's inputView field to use this keyboard.

You'll notice a second private property named delegate, which essentially typecasts the UITextField pointer to a UITextInput pointer. I probably could have done this cast inline, but I sensed this might be useful as a function for future expansion, perhaps to include support for UITextView.

The function dataPress is what inserts text input the edited field using the insertText method of UIKeyInput. This seems to work in all versions back to iOS 4. For my keyboard, I'm simply using the label of each button, which is pretty normal. Use whatever NSStrings strike your fancy.

The function dataBack does the backspace and is a little more complicated. When the UIKeyInput deleteBackward works, it works wonderfully. And while the documentation says it works back to iOS 3.2, it seems to only work back to iOS 5.0, which is when UITextField (and UITextView) conformed to the UITextInput protocol. So prior to that, you're on your own. Since iOS 4 support is a concern to many, I've implemented a lame backspace which works on the UITextField directly. If not for this requirement, I could have made this keyboard work with UITextView. And this backspace isn't as general, only deleting the last character, while deleteBackward will work properly even if the user moves the cursor.

The function enterPress implements the enter key, but is a complete kludge because Apple doesn't seem to give a method for invoking the enter key. So enterPress simply calls the UITextField's delegate function textFieldShouldReturn:, which most programmers implement. Please note that the delegate here is the UITextFieldDelegate for the UITextField and NOT the delegate property for the keyboard itself.

This solution goes around the normal keyboard processing, which hardly matters in the case of UITextField, but makes this technique unusable with UITextView since there is now way to insert line breaks in the text being edited.

That's pretty much it. It took 24 hours of reading and cobbling to make this work. I hope it helps somebody.

iOS Make Custom Keyboard Generate Standard Key Strokes For UITextField

This is a very open ended question. Really, it's up to you to pick what you want. Apple's documentation states this:

The subviews of an input view and input accessory view can be anything you want. If they are buttons or other controls, you need to specify targets and actions for each control and implement the associated action methods to perform data input or manipulation.

You could also make a UITextField subclass and explicitly implement the getter for inputView to return a pointer to your custom keyboard view class. In this getter you can set up a UIControl-like target/action system as you suggest earlier. Doing it this way removes the need to set the custom keyboard from the outside--it's all handled internally. It also puts that code into only one place.

Once the custom text field resigns first responder, you can strip the text field's pointer out of the keyboard.

iOS Custom keyboard can't return focus to app's textfield

The solution is a hack, as of right now you can't really give the host app its focus back.

  • Subclass a UITextField and on its delegate implement
    textFieldShouldBeginEditing by returning NO.

  • Add a BOOL property isSelected that gets set to YES in touchesBegan (not to be confused with the default selected property)

  • In your keyboard's keyPressed method, if searchField.isSelected, manipulate the searchField.text. Else, manipulate textDocumentProxy like normal.
  • Add a clear button and method that wipes searchField.text and searchField.isSelected, allowing any further keystrokes to return to the textDocumentProxy
  • Add an animation that replicates the blinking type cursor

Custom Keyboard on iOS: How do I access the UITextField?

There seems to be no built-in mechanism for this, as the other answerers have pointed out. As Nick says, you don't need a complex delegate pattern for this. Or rather, you use the delegate pattern, but you get the delegate class for free. In this case it's the UITextInput protocol.

So your keyboard probably looks like this (and has a NIB)

@interface ViewController : UIViewController

// use assign if < iOS 5
@property (nonatomic, weak) IBOutlet id <UITextInput> *delegate;

@end

When you create the keyboard controller, you assign the UITextInput conformer to it, so something like this:

- (void)viewDidLoad
{
[super viewDidLoad];

HexKeyboardController *keyboardController = [[HexKeyboardController alloc] initWithNibName:@"HexKeyboardController" bundle:nil];
self.textField.inputView = keyboardController.view;
keyboardController.delegate = self.textField;

}

However, I thought, there MUST be a way to define this keyboard just once and get the keyboard to "automatically know" who the UITextInput object that summoned it is. But I've looked around to no avail... you cannot figure out who the firstResponder is unless you troll the view hierarchy yourself or retain your delegates in a list (which would cause a retain loop). Plus, this isn't so bad because the HexKeyboardController will unload, too, when the textField is dealloced.

iOS 8 custom keyboard on device

I fixed this by turning guided access off in accessibility settings.



Related Topics



Leave a reply



Submit