title: Adopting UIKeyInput protocol to get input from a Bluetooth keyboard
body: <p>I have a Bluetooth foot switch that's basically a wireless keyboard.  One pedal sends the up arrow key, the other sends the down arrow key.  I want to be able to execute my own code in my iPad app when one of the pedals is pressed.  The maker of the pedal tells me I should create a <code>UITextField</code>, and adopt the <code>UIKeyInput</code> protocol in the containing UIView and use the <code>beginningOfDocument</code> and <code>endOfDocument</code> methods to execute my code.  I did this, but no matter what I do, none of the UIKeyInput or UITextInput methods get called.  Can anyone walk me through this, or direct me to a tutorial on something similar to this?  Is there an easier way to do this?</p>
  
  <p>Thanks for your help.</p>
  
  <p>Here's my .h:</p>
  
  <pre><code>#import &lt;UIKit/UIKit.h&gt;
  
  @interface Pedal_ProtocolViewController : UIViewController &lt;UIKeyInput, UITextInput&gt;{
  UITextField *myTextField;
  }
  @property (nonatomic, retain) IBOutlet UITextField *myTextField;
  @end
  </code></pre>
  
  <p>And here's my .m:</p>
  
  <pre><code>#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.
      // e.g. 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 &lt;UITextInputDelegate&gt;) delegate {
  }
  - (id &lt;UITextInputDelegate&gt;) inputDelegate {
      return nil;
  }
  - (id &lt;UITextInputTokenizer&gt;) 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
  </code></pre>