我的应用有一个可选的用户体验,让用户可以在外部键盘上按住修改键(shift、option、command)来引发一些特定的行为。 (当没有连接键盘时,也会在屏幕上提供这种体验。)
UIKeyCommand
需要输入字符串,并在按下组合键时触发一次。当用户按下/释放它们时,我只想跟踪修改键随时间的状态变化。
是否有 iOS API 可以让我跟踪外部键盘上修改键的状态?
最佳答案
您是否尝试过使用空字符串进行输入?
class ViewController: UIViewController {
override var canBecomeFirstResponder: Bool {
return true
}
override var keyCommands: [UIKeyCommand]? {
return [
UIKeyCommand(input: "", modifierFlags: [.shift], action: #selector(shiftPressed(key:))),
UIKeyCommand(input: "", modifierFlags: [.alphaShift], action: #selector(capslockPressed(key:)))
]
}
@objc func shiftPressed(key: UIKeyCommand) {
print("shift pressed \(key)")
}
@objc func capslockPressed(key: UIKeyCommand) {
print("capslock pressed \(key)")
}
}
关于iOS:如何检测外部键盘上修饰键状态的变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54772896/