【问题标题】:Adopting UIKeyInput protocol to get input from a Bluetooth keyboard采用 UIKeyInput 协议从蓝牙键盘获取输入
【发布时间】:2011-09-14 03:10:39
【问题描述】:

我有一个蓝牙脚踏开关,它基本上是一个无线键盘。一个踏板发送向上箭头键,另一个发送向下箭头键。当按下其中一个踏板时,我希望能够在我的 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

【问题讨论】:

  • 我现在也遇到了同样的问题,你解决了吗?
  • Github 上的 DCIntrospect project 中有一些有用的花絮。这些家伙正在使用一些简洁的选择跟踪来区分上、下、左、右以及所有四个,也可以使用 shift 和 option。总而言之,我想,这是一个非常令人印象深刻的努力。

标签: objective-c ios ipad subclass


【解决方案1】:

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

@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 和一个委托来处理输入,或者在UIView 的子类中实现上述两个功能和UIKeyInput 协议。

另请注意,您无需遵守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];
    // ...

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

【讨论】:

  • 其实UIViewControllerNSResponder的子类。 developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/…
  • UIResponder。好吧,我的错 :) 我不确定它是否应该成为第一响应者。但看起来这就是提问者没有做的事情。我会更新我的答案。
  • 不过,问题是我不相信视图控制器可以成为第一响应者,所以你可能仍然需要继承 UIView,但你也必须明确地使其成为触发键盘功能的第一响应者。
  • 当我看到你的评论时,我首先想到的是“视图控制器可能不能成为第一响应者”。但是,我认为 UIKit 所做的唯一检查是询问 UIResponder 它是否可以成为第一响应者。未经测试,我相信上述解决方案(覆盖-canBecomeFirstResponderUIView 也需要)应该是正确的。另外,稍微更新一下答案。
  • 是的,我已经够书呆子了,我去测试了它。我无法让视图控制器成为第一响应者,但 UIView 子类按预期工作。
【解决方案2】:

查看我想出的响应脚踏板方向键的解决方案:

How can I respond to external keyboard arrow keys?

【讨论】:

    【解决方案3】:

    在 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 设备中使用。

    【讨论】:

      猜你喜欢
      • 2010-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-25
      • 1970-01-01
      • 2020-10-14
      • 2014-01-19
      • 1970-01-01
      相关资源
      最近更新 更多