采用UIKeyInput协议从蓝牙键盘获取输入 [英] Adopting UIKeyInput protocol to get input from a Bluetooth keyboard
问题描述
我有一个蓝牙脚踏开关,它基本上是一个无线键盘。一个踏板发送向上箭头键,另一个发送向下箭头键。我希望能够在我的iPad应用程序中按下其中一个踏板时执行我自己的代码。踏板的制造者告诉我应该创建一个 UITextField
,并在包含UIView中采用 UIKeyInput
协议并使用 beginningOfDocument
和 endOfDocument
执行我的代码的方法。我这样做了,但不管我做什么,都不会调用UIKeyInput或UITextInput方法。任何人都可以引导我完成这个,或者指导我一个类似于此的教程吗?有没有更简单的方法呢?
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?
感谢您的帮助。
这是我的.h:
#import <UIKit/UIKit.h>
@interface Pedal_ProtocolViewController : UIViewController <UIKeyInput, UITextInput>{
UITextField *myTextField;
}
@property (nonatomic, retain) IBOutlet UITextField *myTextField;
@end
这是我的.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 {
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
推荐答案
您已在 UIViewController
中采用 UIKeyInput
。请注意您输入的继承定义:
You have adopted the UIKeyInput
in the UIViewController
. Note the inheritance definition you entered:
@interface Pedal_ProtocolViewController : UIViewController <UIKeyInput, UITextInput>{
你在这里说过这是一个实现 UIKeyInput
的视图控制器, UITextInput
。这两个协议适用于 UIResponder
子类,例如 UIView
和子类或 UIView
。 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.
您可以(而不是文本输入协议)只使用隐藏文本字段(例如您已有的字段)。只需创建一个 NSObject
的子类,它实现文本字段的委托,并将其指定为文本字段的委托。然后,在 -viewDidAppear:
中,在文本字段上调用 -becomeFirstResponder
以关注该字段。您可以使用一些黑客来隐藏键盘。
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.
这种方法通常用于游戏和游戏支持库中以显示软件键盘。它甚至适用于iOS 3.1.3及更早版本(考虑到你正在为iPad开发,这对你来说不是问题)。
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];
}
请考虑使用 UITextField
和一个代理处理输入,或在 UIView
的子类中实现上述两个函数和 UIKeyInput
协议。
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
.
另请注意,您不需要符合 UITextInput
只是为了得到按键; UIKeyInput
就足够了。
Also note you are not required to conform to UITextInput
just to get keypresses; UIKeyInput
is enough.
附加说明:如果您决定子类 UIView
(这与我使用隐藏的 UITextField
一样;我没有尝试子类化 UIViewController
获取键盘输入),您需要将 -becomeFirstResponder
添加到 -awakeFromNib
而不是:
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];
}
如果您正在加载 UIViewController 来自笔尖的code>(以及
UIView
)。不这样做?尝试在 -initWithFrame中添加:
:
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;
}
或者,在UIViewController的 viewDidLoad $ c $中c>:
Alternatively, in UIViewController's viewDidLoad
:
// ...
[self.view becomeFirstResponder];
// ...
显然有很多方法可以做到这一点。 ;)
There's obviously loads of ways you can do this. ;)
这篇关于采用UIKeyInput协议从蓝牙键盘获取输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!