iOS Swift蓝牙键盘按键检测

Lea*_*Guy 6 keyboard ios swift

在斯威夫特2在iOS 8.4,我怎样检测时,蓝牙键盘的Up,DownSpace按下按键吧,所以我可以用行动回应?示例代码会很有帮助.

owl*_*ipe 8

对不起,我已经很晚了,我才意识到我可以帮到你.

您需要使用的是UIKeyCommand.看看这个:http://nshipster.com/uikeycommand/

请注意,要检测箭头按键,您需要input: UIKeyInputLeftArrow而不是input: "j"(或等效)显示.您还希望没有修饰符(因此用户不必按CMD-左箭头):请参阅没有修饰符标记的键命令-Swift 2.

基本上,在viewdidload之后(外部)你会想要一些东西:

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

    override var keyCommands: [UIKeyCommand]? {
    return [
        UIKeyCommand(input: UIKeyInputDownArrow, modifierFlags: [], action: "DownArrowPress:"),
        UIKeyCommand(input: UIKeyInputUpArrow, modifierFlags: [], action: "UpArrowPress:"),
        UIKeyCommand(input: " ", modifierFlags: [], action: "SpaceKeyPress:")]
    }

// ...

func DownArrowPress(sender: UIKeyCommand) {
 // this happens when you press the down arrow.   
}
func UpArrowPress(sender: UIKeyCommand) {
 // this happens when you press the up arrow.   
}
func SpaceKeyPress(sender: UIKeyCommand) {
 // this happens when you press the space key.   
}
Run Code Online (Sandbox Code Playgroud)

我希望这会有所帮助,如果您需要更多帮助或某些事情不对,请回复@owlswipe.