首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIKeyCommand在iPadOS 15测试版中的键盘快捷键

UIKeyCommand在iPadOS 15测试版中的键盘快捷键
EN

Stack Overflow用户
提问于 2021-08-10 17:59:20
回答 2查看 999关注 0票数 5

由于某些原因,我无法在iPadOS 15 (beta 5)中使用硬件键盘快捷键。它们适用于大多数键,但不适用于箭头键和选项卡键。

在Xcode 13 (beta 4)编译并在iPadOS 14.5模拟器上运行时,相同的代码似乎工作得很好,但是当使用相同的Xcode构建时,在iPadOS 15 sim上却拒绝工作。我在实际设备上试用过,iPadOS 15 betas最多5次,结果也是一样。

下面是一个很小的例子:

代码语言:javascript
复制
class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        addKeyCommand(UIKeyCommand(title: "UP", action: #selector(handle(key:)), input: UIKeyCommand.inputUpArrow, modifierFlags: []))
        addKeyCommand(UIKeyCommand(title: "DOWN", action: #selector(handle(key:)), input: UIKeyCommand.inputDownArrow, modifierFlags: []))
        addKeyCommand(UIKeyCommand(title: "TAB", action: #selector(handle(key:)), input: "\t", modifierFlags: []))
    }

    @objc func handle(key: UIKeyCommand?) {
        NSLog("Intercepted key: \(key?.title ?? "Unknown")")
    }
}

我没有找到任何相关的报告,也没有打开雷达,所以我怀疑我可能在这里漏掉了什么。如果这是应该报告的,我应该在哪里报告这样的bug?

谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-08-11 22:45:06

显然,有一个新的UIKeyCommand属性wantsPriorityOverSystemBehavior,需要将一些密钥设置为true,比如我在问题中提到的:https://developer.apple.com/documentation/uikit/uikeycommand/3780513-wantspriorityoversystembehavior

票数 7
EN

Stack Overflow用户

发布于 2021-12-03 18:57:18

解决方案确实是使用wantsPriorityOverSystemBehavior。但是,由于您使用的是UIResponder的子类,而不是为已知的键添加键命令,所以可以考虑使用内置方法。它比单独添加更有效,只是在模式上更干净。

代码语言:javascript
复制
class ViewController: UIViewController {

    /// - SeeAlso: UIViewController.viewDidLoad()
    override func viewDidLoad() {
        super.viewDidLoad()
        // do other things, nothing connected with UIKeyCommand
    }

    /// - SeeAlso: UIResponder.keyCommands
    override var keyCommands: [UIKeyCommand]? {
        let commands = [
            UIKeyCommand(input: UIKeyCommand.inputUpArrow, modifierFlags: [], action: #selector(actionUp)),
            UIKeyCommand(input: UIKeyCommand.inputDownArrow, modifierFlags: [], action: #selector(actionDown)),
            UIKeyCommand(input: UIKeyCommand.inputLeftArrow, modifierFlags: [], action: #selector(actionLeft)),
            UIKeyCommand(input: UIKeyCommand.inputRightArrow, modifierFlags: [], action: #selector(actionRight))
        ]
        // if your target is iOS 15+ only, you can remove `if` and always apply this rule
        if #available(iOS 15, *) { 
            commands.forEach { $0.wantsPriorityOverSystemBehavior = true }
        }
        return commands
    }
}

private extension ViewController {
   
    @objc func actionUp() { print("up") }
    @objc func actionDown() { print("down") }
    @objc func actionLeft() { print("left") }
    @objc func actionRight() { print("right") }
}

很抱歉,这是一个答案,但是注释不允许有一个很好的代码语法:-)希望您不介意我添加的内容,但是可能有人不知道keyCommands,并发现它在这个上下文中很有用。至少我给出了一个如何使用API的新部分的例子,创建者@AlexStaravoitau已经写过了这个例子。

苹果声称:

在iOS 15之前,系统首先将键盘事件传递给您的键命令对象,然后传递到文本输入或焦点系统。如果您的应用程序链接到iOS 14 SDK或更早版本,则即使在iOS 15或更高版本上运行时,应用程序也会保留这种行为。

这是一个非常重要的小细节,值得记住。我误解了它,并认为如果您支持iOS 14或更低,它将以旧的方式工作。但这是我的错误理解,它是关于链接的(例如,如果您使用Xcode 12或更高版本构建,并且不在库中添加额外的iOS )。因此,尽管我的应用程序仍然支持iOS 12+,但是键命令不再适用于Xcode 13在iOS 15上的新构建,它只在我修改了上述标志之后才开始工作。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68731570

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档