Support external iOS keyboard without displaying one

Given UIViewController, I would like to receive text input only from an external keyboard. Think UIKeyCommand, but for any character (and not just "changed").

However, when I try to implement this with UIKeyInput, it seems that iOS desperately wants to display the keyboard if there is no external connected.

Is there any way around this? In particular, to be able to receive keystrokes from the keyboard, if and only if one is connected?

+2
source share
1 answer

, iPad , , , . . , UIKeyboardWillShowNotification , iPad, . , :

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

, , UIKeyboardWillShowNotification , - . , - , . , - . , :

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)

, , ,

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

deinit{}, .

:

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()
}

55 - . , . !isFirstBigger , . , diff != 55 , , .

, , , Stack Overflow. - , , , , . , !

+1

Source: https://habr.com/ru/post/1662173/


All Articles