Swift检测外部键盘按键

时间:2017-11-18 17:40:07

标签: ios swift sprite-kit keyboard

我在编写iOS游戏(使用SpriteKit),我想用外部键盘控制它。 我试过那段代码:

    func DownArrowPress(sender: UIKeyCommand) {
    print("Arrow Down Start")
}
func UpArrowPress(sender: UIKeyCommand) {
    print("Arrow Up Start")
}
func LeftArrowPress(sender: UIKeyCommand) {
    print("Arrow Left Start")
}
func RightArrowPress(sender: UIKeyCommand) {
    print("Arrow Right Start")
}
func SpaceKeyPress(sender: UIKeyCommand) {
    print("Space Start")
}
override var keyCommands: [UIKeyCommand]? {
    return [
        UIKeyCommand(input: UIKeyInputDownArrow, modifierFlags: [], action: "DownArrowPress"),
        UIKeyCommand(input: UIKeyInputUpArrow, modifierFlags: [], action: "UpArrowPress"),
        UIKeyCommand(input: UIKeyInputLeftArrow, modifierFlags: [], action: "UIKeyInputLeftArrow"),
        UIKeyCommand(input: UIKeyInputRightArrow, modifierFlags: [], action: "UIKeyInputRightArrow"),
        UIKeyCommand(input: " ", modifierFlags: [], action: "SpaceKeyPress")]
}

但现在我有两个问题:

  1. 我无法检测到按键的结束!

  2. 如果按一个键,没有任何反应......

  3. 您有解决问题的想法吗?

    感谢您的帮助!

0 个答案:

没有答案