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 UITextField
, and adopt the UIKeyInput
protocol in the containing UIView and use the beginningOfDocument
and endOfDocument
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?
Thanks for your help.
Here's my .h:
#import <UIKit/UIKit.h>
@interface Pedal_ProtocolViewController : UIViewController <UIKeyInput, UITextInput>{
UITextField *myTextField;
}
@property (nonatomic, retain) IBOutlet UITextField *myTextField;
@end
And here's 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.
// 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 <UITextInputDelegate>) delegate {
}
- (id <UITextInputDelegate>) inputDelegate {
return nil;
}
- (id <UITextInputTokenizer>) tokenizer {
ret开发者_如何学Gourn 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
You have adopted the UIKeyInput
in the UIViewController
. Note the inheritance definition you entered:
@interface Pedal_ProtocolViewController : UIViewController <UIKeyInput, UITextInput>{
You've said here "This is a view controller that implements UIKeyInput
and UITextInput
." These two protocols apply to UIResponder
subclasses such as UIView
and subclasses or UIView
. UIViewController
is not one such class perhaps not the best class to handle text input.
View controllers manage views. They are not views themselves.
You can (instead of text input protocols) just use a hidden text field (such as the one you already have). Just create a subclass of NSObject
that implements a delegate for the text field, and assign it as the delegate of the text field. Then, in -viewDidAppear:
, call -becomeFirstResponder
on the text field to focus on the field. You can probably use some hack to hide the keyboard.
This approach was commonly used in games and game-supporting libraries to show the software keyboard. It even works on iOS 3.1.3 and earlier (which is not a problem for you, considering that you are developing for the iPad).
In case you will keep that design (handling input in the view controller), then this may be required and make it work.
-(BOOL)canBecomeFirstResponder
{
return YES;
}
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self becomeFirstResponder];
}
Do consider using the UITextField
and a delegate for handling input, or implementing the above two functions and the UIKeyInput
protocol in a subclass of UIView
.
Also note you are not required to conform to UITextInput
just to get keypresses; UIKeyInput
is enough.
Additional note: if you decide to subclass UIView
(which, along with using a hidden UITextField
, is what I do; I haven't tried subclassing UIViewController
to get keyboard input), you will want to add -becomeFirstResponder
to -awakeFromNib
instead:
-(void)awakeFromNib
{
[super awakeFromNib];
[self becomeFirstResponder];
}
That's if you're loading the UIViewController
(and hence the UIView
) from a nib. Not doing that? Try adding that in -initWithFrame:
:
-(id)initWithFrame:(CGFrame)frame
{
self = [super initWithFrame:frame];
if(!self)
return nil;
[self becomeFirstResponder];
return self;
}
Alternatively, in UIViewController's viewDidLoad
:
// ...
[self.view becomeFirstResponder];
// ...
There's obviously loads of ways you can do this. ;)
See the solution I came up with for responding to arrow keys from a foot pedal:
How can I respond to external keyboard arrow keys?
in IOS 7 it is easy. In previous versions - not official. Can be removed from 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];
}
It works for me, hope will be useful for you. Added version check to avoid usage in non-iOS7 devices.
精彩评论