Adopting UIKeyInput protocol for entering information from a Bluetooth keyboard

I have a Bluetooth foot switch, which is basically a wireless keyboard. One pedal sends an up arrow, the other sends a down arrow. I want to be able to execute my own code in my iPad application when one of the pedals is pressed. The pedal maker tells me that I must create a UITextField and accept the UIKeyInput protocol in the containing UIView and use the beginningOfDocument and endOfDocument to execute my code. I did this, but no matter what I do, none of the UIKeyInput or UITextInput methods are called. Can someone get me through this or direct me to a textbook about something like this? Is there an easier way to do this?

Thank you for your help.

Here is my .h:

 #import <UIKit/UIKit.h> @interface Pedal_ProtocolViewController : UIViewController <UIKeyInput, UITextInput>{ UITextField *myTextField; } @property (nonatomic, retain) IBOutlet UITextField *myTextField; @end 

And here is my .m:

 #import "Pedal_ProtocolViewController.h" @implementation Pedal_ProtocolViewController @synthesize myTextField; - (void)dealloc { [super dealloc]; } - (void)didReceiveMemoryWarning { // Releases the view if it doesn't have a superview. [super didReceiveMemoryWarning]; // Release any cached data, images, etc that aren't in use. } #pragma mark - View lifecycle // Implement viewDidLoad to do additional setup after loading the view, typically from a nib. - (void)viewDidLoad { [super viewDidLoad]; [myTextField canBecomeFirstResponder]; [myTextField becomeFirstResponder]; } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. // eg self.myOutlet = nil; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations return YES; } #pragma mark - #pragma mark UIKeyInput Protocol Methods - (BOOL)hasText { return NO; } - (void)insertText:(NSString *)theText { } - (void)deleteBackward { } - (BOOL)canBecomeFirstResponder { return YES; } #pragma mark - #pragma mark UITextInput Protocol Methods - (NSString *)textInRange:(UITextRange *)range { return @""; } - (void)replaceRange:(UITextRange *)range withText:(NSString *)text { } - (void) setSelectedTextRange: (UITextRange *) range { } - (UITextRange *) markedTextRange { return nil; } - (NSDictionary *) markedTextStyle { return nil; } - (void) setMarkedTextStyle: (NSDictionary *) style { } - (void)setMarkedText:(NSString *)markedText selectedRange:(NSRange)selectedRange { } - (void) unmarkText { } - (UITextPosition *) endOfDocument { //DOWN KEY NSLog(@"Down"); return nil; } - (UITextPosition *) beginningOfDocument { //UP KEY NSLog(@"UP"); return nil; } - (UITextRange *)textRangeFromPosition:(UITextPosition *)fromPosition toPosition:(UITextPosition *)toPosition{ return nil; } - (UITextPosition *)positionFromPosition:(UITextPosition *)position offset:(NSInteger)offset{ return nil; } - (UITextPosition *)positionFromPosition:(UITextPosition *)position inDirection:(UITextLayoutDirection)direction offset:(NSInteger)offset { return nil; } - (NSComparisonResult) comparePosition: (UITextPosition *)position toPosition: (UITextPosition *)other { return NSOrderedSame; } - (NSInteger) offsetFromPosition: (UITextPosition *)from toPosition: (UITextPosition *)toPosition { return 0; } - (void) setInputDelegate: (id <UITextInputDelegate>) delegate { } - (id <UITextInputDelegate>) inputDelegate { return nil; } - (id <UITextInputTokenizer>) tokenizer { return nil; } - (UITextPosition *)positionWithinRange:(UITextRange *)range farthestInDirection:(UITextLayoutDirection)direction { return nil; } - (UITextRange *) characterRangeByExtendingPosition: (UITextPosition *) position inDirection: (UITextLayoutDirection) direction { return nil; } - (UITextWritingDirection) baseWritingDirectionForPosition: (UITextPosition *)position inDirection: (UITextStorageDirection)direction { return 0; } - (void) setBaseWritingDirection: (UITextWritingDirection)writingDirection forRange:(UITextRange *)range { } - (CGRect) firstRectForRange: (UITextRange *) range { return CGRectZero; } - (CGRect) caretRectForPosition: (UITextPosition *) position { return CGRectZero; } - (UITextPosition *) closestPositionToPoint: (CGPoint)point { return nil; } - (UITextPosition *) closestPositionToPoint: (CGPoint)point withinRange: (UITextRange *) range { return nil; } - (UITextRange *) characterRangeAtPoint: (CGPoint)point { return nil; } - (UITextRange *) selectedTextRange { return [[UITextRange alloc]init]; } @end 
+8
ios objective-c ipad subclass
source share
3 answers

You have accepted UIKeyInput in the UIViewController . Note the definition of inheritance introduced:

 @interface Pedal_ProtocolViewController : UIViewController <UIKeyInput, UITextInput>{ 

You said here: "This is a view controller that implements UIKeyInput and UITextInput ." These two protocols apply to subclasses of UIResponder , such as UIView and subclasses or UIView . UIViewController not one such class , it may not be the best class for handling text input.

View controllers for managing views. They themselves are not species.

You can (instead of text input protocols) just use a hidden text field (for example, the one you already have). Just create a subclass of NSObject that implements a delegate for the text field and assigns it as a delegate to the text field. Then in -viewDidAppear: call -becomeFirstResponder in the text box to focus on the box. Perhaps you can use some hacks to hide the keyboard.

This approach has been widely used in games and gaming libraries to display a software keyboard. It even works on iOS 3.1.3 and earlier (which is not a problem for you, considering what you are developing for the iPad).


If you save this construct (by processing the input in the view controller), this may be required and make it work.

 -(BOOL)canBecomeFirstResponder { return YES; } -(void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [self becomeFirstResponder]; } 

Consider using a UITextField and a delegate to handle input or implement the above two functions and the UIKeyInput protocol in a subclass of UIView .

Also note that you do not need to match a UITextInput just for receiving keystrokes; UIKeyInput enough.


Additional note: if you decided to subclass UIView (which, along with using the hidden UITextField , is what I am doing, I have not tried subclassing the UIViewController to get keyboard input), you will want to add -becomeFirstResponder to -awakeFromNib instead:

 -(void)awakeFromNib { [super awakeFromNib]; [self becomeFirstResponder]; } 

This is if you download the UIViewController (and therefore the UIView ) from nib. Not this way? Try adding that to -initWithFrame: ::

 -(id)initWithFrame:(CGFrame)frame { self = [super initWithFrame:frame]; if(!self) return nil; [self becomeFirstResponder]; return self; } 

Alternatively, in the UIViewController viewDidLoad :

  // ... [self.view becomeFirstResponder]; // ... 

Here it is obvious that you can do it.;)

+6
source share

Check out the solution I came up with to answer the arrow keys:

How can I answer the arrow keys of an external keyboard?

+2
source share

iOS 7 is easy. In previous versions - not official. It can be removed from the App Store.

 -(NSArray * ) keyCommands { if ([[[UIDevice currentDevice] systemVersion] intValue] <7) return nil; UIKeyCommand *upArrow = [UIKeyCommand keyCommandWithInput: UIKeyInputUpArrow modifierFlags: 0 action: @selector(upArrow:)]; UIKeyCommand *downArrow = [UIKeyCommand keyCommandWithInput: UIKeyInputDownArrow modifierFlags: 0 action: @selector(downArrow:)]; UIKeyCommand *leftArrow = [UIKeyCommand keyCommandWithInput: UIKeyInputLeftArrow modifierFlags: 0 action: @selector(leftArrow:)]; UIKeyCommand *rightArrow = [UIKeyCommand keyCommandWithInput: UIKeyInputRightArrow modifierFlags: 0 action: @selector(rightArrow:)]; UIKeyCommand *escapeCom = [UIKeyCommand keyCommandWithInput:UIKeyInputEscape modifierFlags:0 action:@selector(escapeChar:)]; return [[NSArray alloc] initWithObjects: upArrow, downArrow, leftArrow, rightArrow, escapeCom, nil]; } 

This works for me, hopefully will be useful for you. Version verification added to avoid use on devices other than iOS7.

+2
source share

All Articles