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!

CodingBird
  • 705
  • 2
  • 11
  • 22
  • 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 '15 at 08: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 '15 at 09:09
  • I finally managed to get it working! Thanks for the hint @Paulw11 – CodingBird Aug 18 '15 at 01:55

1 Answers1

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)")
}
CodingBird
  • 705
  • 2
  • 11
  • 22
  • 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. – Hongkun Wang Oct 02 '19 at 00:14
  • any idea of how to make this work in swiftui? – Benjamin B. Dec 14 '20 at 21:35