iOS Swift 蓝牙键盘按键检测

标签 ios swift keyboard

在 iOS 8.4 上的 Swift 2 中,我如何检测何时按下蓝牙键盘的 UpDownSpace 条键,以便我可以用 Action 回应吗? 示例代码会有所帮助。

最佳答案

对不起,我来晚了,我刚刚意识到我可以帮助你。

您需要使用的是 UIKeyCommand。检查一下:http://nshipster.com/uikeycommand/

请注意,要检测箭头键按下,您需要 input: UIKeyInputLeftArrow 而不是 input: "j" (或等价物)。您还希望没有修饰符(这样用户就不必按 CMD-左箭头):请参阅 Key Commands with no modifier flags—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.   
}

我希望这对您有所帮助,如果您需要更多帮助或有不对的地方,请回复@owlswipe。

关于iOS Swift 蓝牙键盘按键检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32274319/

相关文章:

iOS MDM 配置文件签名,使用哪个证书?

iphone - 如何将 iOS 应用程序发送给客户,让他们进行代码签名

ios - 单击 uitableviewcell 时使用 Actionsheet 而不是 segue

swift - Xcode9 是否向后兼容 Swift 3(或 3.1)?

iphone - 图片缓存问题

ios - 评论主 Storyboard上的 View

ios - 如何知道用户何时向上/向下滑动?

ios - Swift iOS 在代码中从键盘上删除麦克风键

iphone - 在 UITextField(iOS) 中检测当前输入的字符

qt - 在 Kiosk 模式下无法使用键盘