[英]iOS detect keyboard layout (e.g. QWERTY, AZERTY)
I am building a custom suggestion/autocorrection feature in an iOS app.我正在 iOS 应用程序中构建自定义建议/自动更正功能。 It must detect accidental adjacent keypresses and compare this to a known word list to suggest the word it thinks the user intended to type.
它必须检测意外的相邻按键并将其与已知单词列表进行比较,以建议它认为用户打算键入的单词。
For example, if the custom word list contains cat
, dog
, monkey
, and the user types cst
, the app can determine that the most likely word was cat
(because s
is adjacent to the a
key)例如,如果自定义单词列表包含
cat
、 dog
、 monkey
,并且用户键入cst
,则应用可以确定最可能的单词是cat
(因为s
与a
键相邻)
This will work on a standard QWERTY keyboard , but what happens if the user is using an AZERTY keyboard?这将适用于标准QWERTY 键盘,但如果用户使用的是 AZERTY 键盘会发生什么?
For the autocorrect/suggest to work reliably, the app must be able to detect the keyboard layout in use.为了使自动更正/建议可靠地工作,应用程序必须能够检测到正在使用的键盘布局。
In iOS, it is possible to obtain aUITextInputMode
object from a UITextField
.在 iOS 中,可以从 UITextField 获得
UITextInputMode
UITextField
。 This object has a primaryLanguage
(string) property, which will display the locale (eg en-GB
), but this does not contain enough granularity to distinguish between English (Australia) QWERTY
and English (Australia) AZERTY
.这个 object 有一个
primaryLanguage
(string) 属性,它将显示语言环境(例如en-GB
),但这不包含足够的粒度来区分English (Australia) QWERTY
和English (Australia) AZERTY
。 In both cases, the primaryLanguage
is en-AU
.在这两种情况下,
primaryLanguage
都是en-AU
。
Is it possible to detect the keyboard layout in iOS?是否可以检测 iOS 中的键盘布局?
I have not been able to find a clean solution to this problem.我一直无法找到一个干净的解决方案来解决这个问题。 Maybe this would be worth a TSI ticket to discuss it with Apple employees.
也许这值得一张 TSI 门票与苹果员工讨论。
I know that this will not be a satisfying answer, but I would still like to share my thoughts here for future readers:我知道这不会是一个令人满意的答案,但我仍然想在这里为未来的读者分享我的想法:
Private API of UITextInputMode : UITextInputMode 的私有 API :
textField.textInputMode?.value(forKey: "identifierWithLayouts")
This will return a string like de_DE@sw=QWERTZ-German;hw=Automatic
from which you can infer the keyboard layout.这将返回一个类似
de_DE@sw=QWERTZ-German;hw=Automatic
的字符串,您可以从中推断出键盘布局。
UserDefaults用户默认值
UserDefaults.standard.object(forKey: "AppleKeyboards")
This will return a list of all keyboards that the user has installed.这将返回用户已安装的所有键盘的列表。 In most cases, this will only be one language (besides the emoji keyboard).
在大多数情况下,这只会是一种语言(除了表情符号键盘)。 For example:
例如:
Optional(<__NSCFArray 0x600003b8e6c0>(en_US@sw=QWERTY;hw=Automatic,emoji@sw=Emoji))
You could also iterate over UserDefaults.standard.dictionaryRepresentation()
and search for QWERTZ/QWERTY/AZERTY within the values.您还可以遍历
UserDefaults.standard.dictionaryRepresentation()
并在值中搜索 QWERTZ/QWERTY/AZERTY。
With much manual effort, you could maybe encode UITextInputModes
to binary data in all ambiguous cases like en_AU.通过大量的手动工作,您可以在 en_AU 等所有模棱两可的情况下将
UITextInputModes
编码为二进制数据。 Something like NSKeyedArchiver.archivedData(withRootObject:textField.textInputMode, requiringSecureCoding: false)
can then be used to compare binary encodings of the user's textInputMode
at runtime.然后可以使用类似
NSKeyedArchiver.archivedData(withRootObject:textField.textInputMode, requiringSecureCoding: false)
的东西在运行时比较用户textInputMode
的二进制编码。
I have found this old question that may have a solution for you.我发现这个老问题可能会为您提供解决方案。 No sure it's still working, but it shows in the question how to get the current instaled keyboards, and someone provided a "gray area" solution, as it seems that there is no direct way to achieve what you intend to do.
不确定它是否仍然有效,但它在问题中显示了如何获取当前安装的键盘,并且有人提供了“灰色区域”解决方案,因为似乎没有直接的方法可以实现您打算做的事情。 Hope this help.
希望这有帮助。
AppleLanguages object at index 0 is common way to get input language.索引 0 处的 AppleLanguages object 是获取输入语言的常用方法。 Instead of trying to determinate which language users are using and default layout, as far I worked with custom keyboard extension, I used one of recommended ways from Apple: use separate keyboard layout for each language.
就我使用自定义键盘扩展而言,我没有尝试确定用户正在使用哪种语言和默认布局,而是使用了 Apple 推荐的一种方法:为每种语言使用单独的键盘布局。 In other way I don't think you will have a stable and productive prediction, auto correct, by the way.
顺便说一句,我认为你不会有一个稳定和富有成效的预测,自动更正。 As for auto correct I used SymSpell ( https://github.com/AmitBhavsarIphone/SymSpell ) and different dictionaries from https://github.com/wolfgarbe/SymSpell/tree/master/SymSpell.FrequencyDictionary to make my own RealmDb for each language.
至于自动更正,我使用了 SymSpell ( https://github.com/AmitBhavsarIphone/SymSpell )和来自https://github.com/wolfgarbe/SymSpell/tree/master/SymDictionary.Frequency的不同字典语。 So far it was a little work to do, but finally my keyboard extension was publisehd in App Store.
到目前为止,还需要做一些工作,但最终我的键盘扩展已在 App Store 中发布。 [Note: I am not related with SynSpell owners or coders] See images
[注意:我与 SynSpell 所有者或编码者无关] 查看图片
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.