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
You have adopted the UIKeyInput
in the UIViewController
. Note the inheritance definition you entered:
@interface Pedal_ProtocolViewController : UIViewController {
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. ;)