iOS Swift蓝牙键盘按键检测 [英] iOS Swift Bluetooth Keyboard keypress detection

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

问题描述

在iOS 8.4上的Swift 2中,如何检测何时按下了蓝牙键盘的UpDownSpace条键,以便我可以做出响应? 示例代码会有所帮助.

In Swift 2 on iOS 8.4 how do I detect when a Bluetooth keyboard's Up, Down, or Space bar keys are pressed so I can respond with an action? Example code would be helpful.

推荐答案

对不起,我来晚了,我才意识到可以为您服务.

Sorry I'm so late, I just realized I can help you.

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

What you need to use is UIKeyCommand. Check this out: http://nshipster.com/uikeycommand/

请注意,要检测到箭头按键,您需要在显示时使用input: UIKeyInputLeftArrow而不是input: "j"(或等价的).您还希望没有修饰符(因此用户不必按CMD左箭头):请参阅

Note that to detect arrow key presses you'll need input: UIKeyInputLeftArrow instead of input: "j" (or equiv.) when that shows up. You'll also want to have no modifiers (so the user won't have to press CMD-left arrow): please refer to Key Commands with no modifier flags—Swift 2.

基本上,在您的viewdidload之后,您会需要一些类似的东西:

Basically, you'll want something along the lines of this after (outside) your 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评论.

I hope this helps, comment back @owlswipe if you need more help or something isn't right.

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

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