4

I have problem to intercept keyboard events. I have connected my iOS with SteelSeries Free (gamepad controller) which when connected to iOS will be detected as a Bluetooth Keyboard. This is tested when I open Notes, any button presses in the gamepad will write a letter.

I need to intercept this button presses and run my own functions but unfortunately I am unable to do so.

I've been trying to use GCController but apparently it is not detected as Game Controller object. When I print the count, it shows as 0. My code below.

let gameControllers = GCController.controllers() as! [GCController]
println("configureConnectedGameControllers count: \(gameControllers.count)")

So I assumed it is because the gamepad is detected as bluetooth keyboard that is why its not detected as game controller. And so I attempted to use UIKeyCommand instead. Below is my code:

override func viewDidLoad() {
    super.viewDidLoad()

    var keys = [UIKeyCommand]()
    for digit in "abcdefghijklmnopqrstuvwxyz"
    {
        keys.append(UIKeyCommand(input:  String(digit), modifierFlags: .Command, action:  Selector("keyPressed:")))
        keys.append(UIKeyCommand(input: String(digit), modifierFlags: .Control, action:  Selector("keyPressed:")))
        keys.append(UIKeyCommand(input: String(digit), modifierFlags: nil, action:  "pressKey"))
    }
}

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

func keyPressed(command: UIKeyCommand) {
    println("another key is pressed") //never gets called
}

func pressKey() {
    println("a key is pressed")
}

But even with the above implementation, nothing is printed in the console when i press a button at the gamepad.

This confuses me. So please help me if you know any answer to this. Thanks in advance!

3
  • You need to return the UIKeyCommand instances from a UIResponder subclass - You could, for example, subclass UIView and make an instance of that your root view for this view controller
    – Paulw11
    Aug 17, 2015 at 8:46
  • Sorry I dont really get iOS / Swift as I just learnt a few days ago. Can you explain it more or if possible provide a sample? @Paulw11
    – CodingBird
    Aug 17, 2015 at 9:09
  • I finally managed to get it working! Thanks for the hint @Paulw11
    – CodingBird
    Aug 18, 2015 at 1:55

1 Answer 1

6

I finally managed to get it working. Below is the code if anyone ever needs it.

var keys = [UIKeyCommand]()

override func viewDidLoad() {
    super.viewDidLoad()
    //configureGameControllers()

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

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

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


func keyPressed(command: UIKeyCommand) {
    println("user pressed \(command.input)")
}
2
  • I also try to handle keyboard event on iPad/iPhone. Your solution solved my problem! Wondering if you know how to detect the "key-down" and "key-up" state. The reason I ask if because this state is very important for a game, for example, a game need to detect whether the arrow keys are currently pressed or released. I have not find answer yet. Oct 2, 2019 at 0:14
  • any idea of how to make this work in swiftui? Dec 14, 2020 at 21:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.