首页 > 解决方案 > 在催化剂程序中检测物理键盘按键

问题描述

我有一个小的 iOS 应用程序,我想用 Catalyst 为 macOS 编译它。该应用程序在 Mac 上可以正常运行,但它是一个计算器应用程序,因此我希望除了单击按钮之外还能够使用键盘输入数字。

我搜索并发现 pressesBegan 的覆盖是这样做的好方法。但是,如果我在https://developer.apple.com/documentation/uikit/mac_catalyst/handling_key_presses_made_on_a_physical_keyboard?changes=_8 像这样实现 Apple 的示例

    override func pressesBegan(_ presses: Set<UIPress>, with event: UIPressesEvent?) {
        // Run backward or forward when the user presses a left or right arrow key.

        var didHandleEvent = false
        for press in presses {
            guard let key = press.key else { continue }
            if key.charactersIgnoringModifiers == UIKeyCommand.inputLeftArrow {
                //runBackward()
                didHandleEvent = true
            }
            if key.charactersIgnoringModifiers == UIKeyCommand.inputRightArrow {
                //runForward()
                didHandleEvent = true
            }
        }

        if didHandleEvent == false {
            // Didn't handle this key press, so pass the event to the next responder.
            super.pressesBegan(presses, with: event)
        }
    }

在我的 Appdelegate 课程中,我的指令出错key = press.key

'UIPres' 类型的值没有成员 'key'

阅读 UIPress 类的文档,我在 XCode 文档中看不到任何关键成员(UIKit>Touches、Presses 和 Gesture>UIPress),但我在 https://developer.apple.com/documentation/uikit/上看到了它uipress ??

我在 Internet 上没有找到任何有关“'UIPress' 类型的值没有成员'键'”消息的报告

标签: iosswiftkeyboardmac-catalyst

解决方案


我发现了我的问题。

我有一个旧版本的 XCode (11.3.1),它不知道 UIPress.key。

升级到 XCode 11.4.1 后,一切正常。

我不太明白原因,因为我可以在属性密钥可用之前为 iOS 12.4 编译代码(Apple 的文档声称 iOS 13.4+)。但它现在有效!


推荐阅读