繁体   English   中英

iOS:如何检测外部键盘上的修改键状态变化?

[英]iOS: How do I detect modifier key state changes on an external keyboard?

我的应用程序具有可选的用户体验,可让用户按住外部键盘上的修饰键(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)")
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM