iOS Swift中断键盘事件 [英] iOS Swift Interrupt Keyboard Events

查看:170
本文介绍了iOS Swift中断键盘事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

拦截键盘事件时遇到问题。我已经将我的iOS连接到SteelSeries Free(游戏手柄控制器),当连接到iOS时,它将被检测为蓝牙键盘。这是在我打开Notes时测试的,在游戏手柄中按下的任何按钮都会写一封信。

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.

我一直在尝试使用GCController,但显然它没有被检测为游戏控制器对象。当我打印计数时,它显示为0.我的代码如下。

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)")

所以我认为这是因为游戏手柄被检测为蓝牙键盘,这就是为什么它没有被检测为游戏控制器。所以我尝试使用UIKeyCommand。下面是我的代码:

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!

推荐答案

我终于成功了。如果有人需要,下面是代码。

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

这篇关于iOS Swift中断键盘事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆