处理外部键盘按下而不在iOS应用程序的屏幕上显示键盘

时间:2017-12-04 22:44:15

标签: ios swift keyboard remote-control

我有一个私人iOS应用程序,我想使用通过蓝牙连接的外部键盘进行控制。我不想在我的应用中使用UITextFieldUITextView,或者至少我不希望键盘一直显示。有没有办法让我从物理键盘听这些键盘事件?感谢。

2 个答案:

答案 0 :(得分:1)

<强> UIKeyCommands

要从键盘上听键,您可以覆盖ViewController上的keyCommands变量(它是一个数组,因此您可以放置​​许多快捷键):

override var keyCommands: [UIKeyCommand]? {
    return [
        UIKeyCommand(input: "Q",
                     modifierFlags: [],
                     action: #selector(self.close),
                     discoverabilityTitle: "Close app")
    ]
}

func close() {
    exit(0)
}

输入是使用的关键,例如CMD + Q(仅放Q)。 Action是调用的选择器和discoverabilityTitle是快捷方式的标题,例如&#34;关闭应用程序&#34;。当用户持有CMD密钥时,它将显示在iPad中。

它仅适用于CMD键和其他特殊键:

UIKeyInputUpArrow
UIKeyInputDownArrow
UIKeyInputLeftArrow
UIKeyInputRightArrow
UIKeyInputEscape

答案 1 :(得分:0)

这对我很有帮助:https://www.avanderlee.com/swift/uikeycommand-keyboard-shortcuts/

我必须添加canBecomeFirstResponder覆盖才能使其正常工作。

相关问题