首页 > 解决方案 > 检测没有修饰符的按键

问题描述

我想在我的 Catalyst 应用程序中捕获按键。

UIKeyCommand(input: "", modifierFlags: nil, action: #selector(singleShift))

不幸的是,modifierFlags 不能为 nil,并且 Catalyst 不支持 NSEvent。有没有办法检测单键?例如键盘应用程序或游戏?

标签: mac-catalystuikeycommand

解决方案


该字段modifierFlags不是可选的,因此您不能通过nil. 它的类型UIKeyModifierFlags是 an OptionSet,所以如果你不想传递修饰符,你可以简单地传递一个空数组。

class ViewController: UIViewController {
    override var keyCommands: [UIKeyCommand]? {
        [UIKeyCommand(input: UIKeyCommand.inputPageUp,
                      modifierFlags: [],
                      action: #selector(pageUpPressed(_:)))]
    }
}

推荐阅读