1

我的应用程序有一个可选的用户体验,让用户按住外部键盘上的修饰键(shift、option、command)来引发一些特定的行为。(当未连接键盘时,也会在屏幕上提供这种体验。)

UIKeyCommand需要输入字符串,并在按下组合键时触发一次。当用户按下/释放它们时,我想跟踪修改键随时间的状态变化。

是否有 iOS API 可以让我跟踪外部键盘上修饰键的状态?

4

1 回答 1

3

您是否尝试过使用空字符串进行输入?

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)")
    }
}
于 2019-02-20T08:38:07.173 回答