简体   繁体   English

iOS 支持外接键盘不显示一个

[英]iOS Support External Keyboard Without Displaying One

Given a UIViewController , I would like to receive text input only from the external keyboard.鉴于UIViewController ,我只想从外部键盘接收文本输入。 Think UIKeyCommand but for any character (not just 'modified' ones).想想UIKeyCommand但对于任何字符(不仅仅是“修改”的字符)。

However, when I try to implement that using UIKeyInput , it seems that iOS desperately wants to display a keyboard if there is no external one connected.但是,当我尝试使用UIKeyInput实现它时,如果没有连接外部键盘,iOS 似乎非常想显示一个键盘。

Is there any way to circumvent that?有什么办法可以规避吗? Specifically, to have the options to receive keystrokes from the keyboard if, and only if, one is connected?具体来说,当且仅当连接一个键盘时,才能选择从键盘接收击键?

After fiddling with a iPad for an hour, I finally have a good solution for this in swift.在摆弄 iPad 一个小时后,我终于迅速找到了一个很好的解决方案。 The other methods are weak or use 3rd party software.其他方法较弱或使用第 3 方软件。 The reason why UIKeyboardWillShowNotification is getting fired even when an external keyboard is being used for an iPad is the shortcut bar existing.即使在 iPad 使用外部键盘时UIKeyboardWillShowNotification也会被触发的原因是现有的快捷方式栏。 In order to disable the shortcut bar, do this:要禁用快捷栏,请执行以下操作:

    let item : UITextInputAssistantItem = textField.inputAssistantItem
    item.leadingBarButtonGroups = []
    item.trailingBarButtonGroups = []

This covers most cases of what you need, but UIKeyboardWillShowNotification can still be fired if someone plugs their keyboard in at certain points of use.这涵盖了您需要的大多数情况,但如果有人在某些使用点插入键盘, UIKeyboardWillShowNotification仍然可以触发。 If you have the screen adjust, you can't afford any case for the user to experience this.如果你有屏幕调整,你不能让用户体验到任何情况。 Plus, you might want the shortcut bar for some reason.另外,出于某种原因,您可能需要快捷方式栏。 Regardless of what your desires are, this covers all cases of an external keyboard being used:无论您的愿望是什么,这都涵盖了使用外部键盘的所有情况:

Add to viewDidAppear添加到viewDidAppear

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(LoginViewController.keyboardWillShow), name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(LoginViewController.keyboardWillHide), name: UIKeyboardWillHideNotification, object: nil)

whenever you leave the view add this to anything that makes you leave每当您离开视图时,将此添加到让您离开的任何内容中

NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil)

also add it to the deinit{} method to be thourough.还将其添加到deinit{}方法中以进行deinit{}

Now use these functions:现在使用这些功能:

func keyboardWillShow(notification: NSNotification) {
    //   This code is an alternate way of checking for keyboard
    var userInfo: [NSObject: AnyObject] = notification.userInfo!
    let firstFrame = userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue
    let secondFrame = userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue
    let firstRect = firstFrame.CGRectValue()
    let secondRect = secondFrame.CGRectValue()
    let diff = abs(firstRect.origin.y - secondRect.origin.y)
    let isFirstBigger = firstRect.origin.y > secondRect.origin.y
    if firstRect != secondRect && diff != 55 {
        if !isFirstBigger {
            //animateViewToDefaultPosition()
        } else {
            //animateViewToPositionWhenKeyboardActive()
        }
    }
}

func keyboardWillHide() {
    //animateViewToDefaultPosition()
}

The 55 is the height of the shortcut bar. 55 是快捷栏的高度。 You can remove it's functionality if you don't have one.如果你没有它,你可以删除它的功能。 The !isFirstBigger is used to check for when they unhook the keyboard and hook it back in during text field editing. !isFirstBigger用于检查他们何时在文本字段编辑期间解开键盘并将其重新连接。 It is also important that diff != 55 during that check because with a shortcut bar this would be the case when you did not want to animate the screen.在该检查期间diff != 55也很重要,因为对于快捷栏,当您不想为屏幕设置动画时就会出现这种情况。

This is by far the best method I have seen after scouring Stack Overflow.这是迄今为止我在搜索 Stack Overflow 后看到的最好的方法。 If anyone finds a bug in the functionality, let me know, but I am confident it will take care of the pesky shortcut bar external keyboard issues.如果有人发现功能中的错误,请告诉我,但我相信它会解决讨厌的快捷方式栏外部键盘问题。 I hope this helps everyone else out there confused by all this!我希望这可以帮助其他所有人都被这一切弄糊涂了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM