2

My IOS app need to handle a connected bluetooth keyboard to do some function in the app, I tried this method, but the xcode cannot found the Cocoa

Then I tried this method, this time xcode cannot find the canBecomeFirstResponder in super class.

How can I capture keyboard event in my app?

import UIKit

class ViewController: UIViewController {

    var keys = [UIKeyCommand]()
    override func viewDidLoad() {
        super.viewDidLoad()


        for digit in "1234567890abcdefghijklmnopqrstuvwxyz"
        {
            keys.append(UIKeyCommand(input: String(digit), modifierFlags: [], action:  Selector(("keyPressed:"))))
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    override func canBecomeFirstResponder() -> Bool {
        return true
    }

    override var keyCommands: [AnyObject]? {
        get {
            return keys
        }
    }


    func keyPressed(command: UIKeyCommand) {
        print("user pressed \(command.input)")
    }

}
NSNoob
  • 5,548
  • 6
  • 41
  • 54
CL So
  • 3,647
  • 10
  • 51
  • 95

0 Answers0