在 iOS 8.4 上的 Swift 2 中,我如何检测何时按下蓝牙键盘的 Up
、Down
或 Space
条键,以便我可以用 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/