首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >采用UIKeyInput协议实现蓝牙键盘的输入

采用UIKeyInput协议实现蓝牙键盘的输入
EN

Stack Overflow用户
提问于 2011-09-14 11:10:39
回答 2查看 11.1K关注 0票数 8

我有一个蓝牙脚踏开关,基本上就是一个无线键盘。一个踏板发送向上箭头键,另一个发送向下箭头键。我希望当其中一个踏板被按下时,能够在我的iPad应用程序中执行我自己的代码。踏板的制造商告诉我应该创建一个UITextField,并在包含的UIView中采用UIKeyInput协议,并使用beginningOfDocumentendOfDocument方法来执行我的代码。我这样做了,但无论我做什么,都不会调用任何UIKeyInput或UITextInput方法。有没有人可以给我介绍一下,或者给我一个类似的教程?有没有更简单的方法来做这件事?

谢谢你的帮助。

这是我的.h:

代码语言:javascript
复制
#import <UIKit/UIKit.h>

@interface Pedal_ProtocolViewController : UIViewController <UIKeyInput, UITextInput>{
UITextField *myTextField;
}
@property (nonatomic, retain) IBOutlet UITextField *myTextField;
@end

下面是我的.m:

代码语言:javascript
复制
#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
EN

回答 2

Stack Overflow用户

发布于 2011-11-07 20:35:25

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

How can I respond to external keyboard arrow keys?

票数 2
EN

Stack Overflow用户

发布于 2013-11-12 18:39:58

在iOS7中,这很容易。在以前的版本中--非官方版本。可以从App Store中删除。

代码语言:javascript
复制
-(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设备中使用。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7410854

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档