【问题标题】:How do I detect that a hardware keyboard is attached to an iPhone? [duplicate]如何检测到硬件键盘已连接到 iPhone? [复制]
【发布时间】:2011-03-10 17:03:23
【问题描述】:

可能重复:
iPad: detect if external keyboard is present

我一直在参考库中四处寻找,但似乎无法在这里找到答案。

我假设某处有一些 API,我可以查询以了解是否正在使用外部硬件键盘。

更新 我刚刚尝试了 ExternalAccessory.framework 中的EAAccessoryManager.connectedAccessories。那是不行的,当硬件键盘启用时它返回一个空数组。

【问题讨论】:

  • 只是好奇:您为什么需要这些信息?
  • 启用硬件键盘后,屏幕键盘不再显示。我们正在为我们拥有的使用日期的文本字段添加一个日期选择器作为键盘上的子视图。由于使用了 Three20 TTMessageController 基础设施,该基础设施只允许 UITextFields 作为消息中的字段,因此这是一种 hack。当用户触摸我们的“日期”文本字段时,我们会找到键盘并覆盖 UIDatePicker。
  • 确定在滚动输入时是否需要考虑虚拟键盘的大小。
  • user174448 和 @jamone- 你们有没有找到解决问题的方法,让日期选择器替换屏幕键盘并需要处理案例/或检测硬件键盘?
  • 实际上,我在模拟器中看到的是,如果您正在设置 uitextfield 的 inputView 属性,那么 iOS 无论如何都知道要显示键盘。听起来这会解决你的情况。不确定这是否是最初询问此 q 时的一个选项,但我认为我还是会发布

标签: iphone ios


【解决方案1】:

我认为你必须使用以下代码 -

- (void)viewDidLoad {
    UIView*  _noExternalAccessoriesPosterView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [_noExternalAccessoriesPosterView setBackgroundColor:[UIColor whiteColor]];
    _noExternalAccessoriesLabelView = [[UILabel alloc] initWithFrame:CGRectMake(60, 170, 240, 50)];
    [_noExternalAccessoriesLabelView setText:@"No Accessories Connected"];
    [_noExternalAccessoriesPosterView addSubview:_noExternalAccessoriesLabelView];
    [[self view] addSubview:_noExternalAccessoriesPosterView];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_accessoryDidConnect:) name:EAAccessoryDidConnectNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_accessoryDidDisconnect:) name:EAAccessoryDidDisconnectNotification object:nil];
    [[EAAccessoryManager sharedAccessoryManager] registerForLocalNotifications];

    _eaSessionController = [EADSessionController sharedController];
    _accessoryList = [[NSMutableArray alloc] initWithArray:[[EAAccessoryManager sharedAccessoryManager] connectedAccessories]];

    [self setTitle:@"Accessories"];

    if ([_accessoryList count] == 0) {
        [_noExternalAccessoriesPosterView setHidden:NO];
    } else {
        [_noExternalAccessoriesPosterView setHidden:YES];
    }
}

- (void)_accessoryDidConnect:(NSNotification *)notification {
    EAAccessory *connectedAccessory = [[notification userInfo] objectForKey:EAAccessoryKey];
    [_accessoryList addObject:connectedAccessory];

    if ([_accessoryList count] == 0) {
        [_noExternalAccessoriesPosterView setHidden:NO];
    } else {
        [_noExternalAccessoriesPosterView setHidden:YES];
    }

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:([_accessoryList count] - 1) inSection:0];
    [[self tableView] insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}

希望这对你有用,记住你必须使用 ExternalAccessory 框架来编写这段代码。

【讨论】:

  • Xcode 添加 ExternalAccessory.framework 后变得疯狂。
猜你喜欢
  • 2015-11-13
  • 1970-01-01
  • 1970-01-01
  • 2011-03-20
  • 2012-10-27
  • 2023-03-06
  • 1970-01-01
  • 1970-01-01
  • 2010-09-18
相关资源
最近更新 更多