【问题标题】:How do I know if there's a hardware keyboard? [duplicate]我怎么知道是否有硬件键盘? [复制]
【发布时间】:2012-04-18 07:11:30
【问题描述】:

可能重复:
How can I detect if an external keyboard is present on an iPad?

当用户开始在我的 iPhone 应用程序的 UI 中编辑文本字段时,我想使用文本字段和键盘之间的空间来列出相关选项。

使用 iOS 中的常规(屏幕)键盘,有几个 convenient notifications 可以听,这有助于我决定我为中间的列表留出多少空间。

但是,当您连接(或模拟使用)硬件键盘时,这些通知将不再发布。不是太意外,但这给我带来了一些挑战。如果用户使用硬件键盘,我有更多的空间用于列表。

但是,如果没有触发通知或委托方法可听,我如何确定用户是否有硬件键盘?或者有吗?

还需要考虑的是,用户可以先通过软件键盘使用该应用程序,然后连接他/她的硬件键盘并继续使用该应用程序。此时,软键盘将隐藏,但用户仍在编辑文本字段。

我为这个问题找到了一个老骗子,但没有答案:

【问题讨论】:

    标签: objective-c ios cocoa-touch keyboard


    【解决方案1】:

    如果连接了硬件键盘(否则它们连接到屏幕键盘的顶部),输入附件视图将显示在屏幕底部。

    将输入视图的附属视图设置为大小为CGRectZero 的空UIView。您将收到两种情况的显示/隐藏通知,并能够从metrics in the notification's userinfo dictionary 确定可用的屏幕尺寸。

    UIView *inputAccessoryView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
    inputAccessoryView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    textView.inputAccessoryView = inputAccessoryView;
    
    …
    
    - (void)keyboardWillShow:(NSNotification*)aNotification {
      NSLog(@"%@", [aNotification userInfo]);
    }
    

    记录的指标 - 注意UIKeyboardFrameEndUserInfoKey 中的框架不在屏幕上

    // (NSDictionary *) {
    //     UIKeyboardAnimationCurveUserInfoKey = 0;
    //     UIKeyboardAnimationDurationUserInfoKey = "0.25";
    //     UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {768, 264}}";
    //     UIKeyboardCenterBeginUserInfoKey = "NSPoint: {384, 891}";
    //     UIKeyboardCenterEndUserInfoKey = "NSPoint: {384, 1155}";
    //     UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 758}, {768, 264}}";
    //     UIKeyboardFrameChangedByUserInteraction = 0;
    //     UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 1022}, {768, 264}}";
    // }
    

    更新 - 与相关问题中的 user721239's answer 非常相似。

    【讨论】:

    • 不错的技巧,确实是另一个问题的欺骗,但这并没有出现在我的搜索中。谢谢!
    猜你喜欢
    • 2021-10-05
    • 1970-01-01
    • 2021-06-10
    • 2011-04-03
    • 2014-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多