采用UIKeyInput协议从蓝牙键盘获取输入

时间:2011-09-14 03:10:39

标签: objective-c ios ipad subclass

我有一个蓝牙脚踏开关,它基本上是一个无线键盘。一个踏板发送向上箭头键,另一个发送向下箭头键。我希望能够在我的iPad应用程序中按下其中一个踏板时执行我自己的代码。踏板的制作者告诉我应该创建一个UITextField,并在包含UIView中采用UIKeyInput协议,并使用beginningOfDocumentendOfDocument方法来执行我的代码。我这样做了,但不管我做什么,都不会调用UIKeyInput或UITextInput方法。任何人都可以引导我完成这个,或者指导我学习与此相似的教程吗?有更简单的方法吗?

感谢您的帮助。

这是我的.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

3 个答案:

答案 0 :(得分:6)

您已采用UIKeyInput中的UIViewController。请注意您输入的继承定义:

@interface Pedal_ProtocolViewController : UIViewController <UIKeyInput, UITextInput>{

你在这里说过“这是一个实现UIKeyInputUITextInput的视图控制器。”这两个协议适用于UIResponder子类,例如UIView和子类或UIViewUIViewController 不是一个这样的类可能不是处理文本输入的最佳类。

查看控制器管理视图。他们不是自己的观点。

您可以(而不是文本输入协议)只使用隐藏文本字段(例如您已有的字段)。只需创建一个NSObject的子类,它实现文本字段的委托,并将其指定为文本字段的委托。然后,在-viewDidAppear:中,在文本字段上调用-becomeFirstResponder以关注该字段。您可以使用一些黑客来隐藏键盘。

这种方法通常用于游戏和游戏支持库中以显示软件键盘。它甚至适用于iOS 3.1.3及更早版本(考虑到您正在为iPad开发,这对您来说不是问题。)


如果您将保留该设计(在视图控制器中处理输入),则可能需要这样做并使其正常工作。

-(BOOL)canBecomeFirstResponder
{
    return YES;
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    [self becomeFirstResponder];
}

请考虑使用UITextField和委托处理输入,或在UIKeyInput的子类中实现上述两个函数和UIView协议。

另请注意,您不需要遵守UITextInput只是为了获得按键; UIKeyInput就足够了。


附加说明:如果您决定继承UIView(与使用隐藏的UITextField一起,我就是这样;我没有尝试继承UIViewController来获取键盘输入),您需要将-becomeFirstResponder添加到-awakeFromNib,而不是:

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

如果您从笔尖加载UIViewController(以及UIView),那就是这样。不这样做?尝试在-initWithFrame:中添加:

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

    [self becomeFirstResponder];
    return self;
}

或者,在UIViewController的viewDidLoad

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

显然有很多方法可以做到这一点。 ;)

答案 1 :(得分:2)

请参阅我提出的用于响应脚踏板上的箭头键的解决方案:

How can I respond to external keyboard arrow keys?

答案 2 :(得分:2)

在IOS 7中很容易。在以前的版本 - 不官方。可以从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];
}

它对我有用,希望对你有用。添加了版本检查以避免在非iOS7设备中使用。