Matic1911
Matic1911

Reputation: 391

iOS Swift detect keyboard events

Can I somehow detect events from iOS keyboard?

I would like to detect this kind of events on the UIViewController which does not have UITextField or any sort of this kind of objects.

All I have are four circles that are UIView and I would like to paint them in different colour when the button on the keyboard is pressed.

enter image description here

Upvotes: 12

Views: 20684

Answers (3)

Ayman Ibrahim
Ayman Ibrahim

Reputation: 1399

Swift 4.2

Register for events:

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: UIResponder.keyboardWillShowNotification , object:nil)

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: UIResponder.keyboardWillHideNotification , object:nil)

Selectors:

@objc func keyboardWillShow(notification: NSNotification) {
   let keyboardHeight = (notification.userInfo![UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue.height
   print(keyboardHeight)
}

@objc func keyboardWillHide(notification: NSNotification) {
   let keyboardHeight = (notification.userInfo![UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue.height
   print(keyboardHeight)
}

Upvotes: 13

0x384c0
0x384c0

Reputation: 2284

You can create your own View with keyboard handler

//Example
class CustomKeyInput: UIControl, UIKeyInput{
    //UIKeyInput
    public var hasText: Bool { return false }
    public func insertText(_ text: String){
        print(#function)
        print(text)
        print(text.characters.count)
    }
    public func deleteBackward(){
        print(#function)
    }

    //UIControl
    override var canBecomeFirstResponder: Bool {return true}
}


//Usage
class ViewController: UIViewController {

    @IBOutlet weak var keyView: CustomKeyInput!

    override func viewDidAppear(_ animated: Bool) {
        keyView.becomeFirstResponder()
    }
}

Upvotes: 3

Jitendra Solanki
Jitendra Solanki

Reputation: 772

You are not having any object to take the input from the keyboard. To make the keyboard appear, you must have either UITextField or UITextView object in your view.

But here is the process to detect the keyboard events in your view controller. You can set observers on view controller for keyboard notifications.

To register for keyboard notification in swift 3, use write these lines in viewDidLoad() or viewWillAppear() method of your view controller

    NotificationCenter.default.addObserver(self, selector: #selector(keyBoardWillShow(notification:)), name: .UIKeyboardWillShow, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(keyBoardWillHide(notification:)), name: .UIKeyboardWillHide, object: nil)

And then implement two methods keyBoardWillHide(:) and keyBoardWillShow(:) method in your view controller

    func keyBoardWillShow(notification: NSNotification) {
            //handle appearing of keyboard here
    }


    func keyBoardWillHide(notification: NSNotification) {
              //handle dismiss of keyboard here
     }

Upvotes: 16

Related Questions