Use translated key labels when identifying emulated system keyboard keys #6212#6267
Conversation
…rsion of a key is now spoken instead of the raw, untranslated version. Also, the script name for emulated system keyboard keys is now localized Both changes are based on a new simple function called getKeyCombinationLabel in keyLabels in which you insert a raw key combination string, which is than converted to a localised key combination identifier. (Re #6212)
| 'tab': pgettext("keyLabel", "tab"), | ||
| } | ||
|
|
||
| def getKeyCombinationLabel(keyCombination): |
There was a problem hiding this comment.
Please add a docstring explaining what this does. It need only be one or two lines.
There was a problem hiding this comment.
Done.
Furthermore, I noticed that my code failed if a particular key wasn't in localizedKeyLabels. When that happens now, the code falls back to the lowercase version of the originally provided key. Yay for list comprehension power!
| scriptName = scriptName.encode("mbcs") | ||
| func.__name__ = "script_%s" % scriptName | ||
| func.__doc__ = _("Emulates pressing %s on the system keyboard") % keyName | ||
| func.__doc__ = _("Emulates pressing %s on the system keyboard") % keyLabels.getKeyCombinationLabel(keyName) |
There was a problem hiding this comment.
You can instead use emuGesture.displayName here. This does more or less the same thing, but it is the same code used to retrieve display names for normal keyboard keys in input help, and since we have the gesture, we may as well use it. There's actually a slight difference in the output too because it accounts for the set ordering that NVDA uses for modifier keys. You could of course do this in your keylabels function, but it's not really important.
You still need your keyLabels function for the Gestures dialog stuff because we don't build the actual gesture in that case, so we can't use the displayname property.
…mbinationLabel would fail if a particular key, such as f4 which is not in keyLabels.localizedKeyLabels, was asked for.
| @returns: A localized key combination | ||
| @rtype: string | ||
| """ | ||
| return "+".join(localizedKeyLabels[key.lower()] if key.lower() in localizedKeyLabels.keys() else key.lower() for key in keyCombination.split("+")) |
There was a problem hiding this comment.
- This certainly demonstrates the power of comprehensions :), but it does mean you end up calling
key.lower()twice per key. - Calling
dict.keysgenerates a list of keys you then have to search, which is unnecessary here. For future reference, usekey in dict, notkey in dict.keys(). - You can eliminate the existence check by using
dict.getwith a default.
While premature optimisation is the root of all evil, I think it's worth eliminating unnecessary calls, especially if it makes things more readable. Perhaps something like this:
keys = keyCombination.lower().split("+")
return "+".join(localizedKeyLabels.get(key, key) for key in keys)
This pull request includes the following:
Both changes are based on a new simple function in keyLabels in which you insert a raw key combination string, which is than converted to a localised key combination identifier.
Fixes #6212.
What's New entry: in Bug fixes: