ios - 在催化剂程序中检测物理键盘按键
问题描述
我有一个小的 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' 类型的值没有成员'键'”消息的报告
解决方案
我发现了我的问题。
我有一个旧版本的 XCode (11.3.1),它不知道 UIPress.key。
升级到 XCode 11.4.1 后,一切正常。
我不太明白原因,因为我可以在属性密钥可用之前为 iOS 12.4 编译代码(Apple 的文档声称 iOS 13.4+)。但它现在有效!
推荐阅读
- python - 在等高线上显示下坡的刻度线
- python - 从混淆矩阵中计算真正值以进行多类分类
- windows - 进程资源管理器未在 Win10 x64 上显示 32 位进程的 32 位堆栈
- oracle - 透视查询的列中的不同值
- java - 事务管理器不回滚 Spring Batch 作业
- scala - 检查我的列表是否满足 scala 函数中嵌套循环中设置的参数
- android - 在 Android 设备上播放来自 ASIO 设备的音频
- javascript - 从组件外部的 html 按钮调用 store.dispatch
- wordpress - 滑块革命 YouTube 视频不起作用
- reactjs - JSX 元素类型“Element[]”不是 JSX 元素的构造函数