diff --git a/src/app_model/types/_keys/_key_codes.py b/src/app_model/types/_keys/_key_codes.py index 8594efcf..7edb965c 100644 --- a/src/app_model/types/_keys/_key_codes.py +++ b/src/app_model/types/_keys/_key_codes.py @@ -595,11 +595,7 @@ class _KM(NamedTuple): SCANCODE_FROM_LOWERCASE_STRING: Dict[str, ScanCode] = {} KEYCODE_TO_STRING: Dict[KeyCode, str] = {} - KEYCODE_FROM_LOWERCASE_STRING: Dict[str, KeyCode] = { - # two special cases for assigning os-specific strings to the meta key - 'win': KeyCode.Meta, - 'cmd': KeyCode.Meta, - } + KEYCODE_FROM_LOWERCASE_STRING: Dict[str, KeyCode] = {} # key symbols on all platforms KEY_SYMBOLS: dict[KeyCode, str] = { @@ -660,6 +656,11 @@ class _KM(NamedTuple): if km.virtual_key: _NATIVE_WINDOWS_VK_TO_KEYCODE[km.virtual_key] = km.keycode + # also parse the os-specific symbols and names returned by `to_text` + for mapping in (*OS_KEY_SYMBOLS.values(), *OS_KEY_NAMES.values()): + for keycode, string in mapping.items(): + KEYCODE_FROM_LOWERCASE_STRING.setdefault(string.lower(), keycode) + def _keycode_to_string(keycode: KeyCode) -> str: """Return the string representation of a KeyCode.""" # sourcery skip diff --git a/src/app_model/types/_keys/_keybindings.py b/src/app_model/types/_keys/_keybindings.py index 43c9cf40..7a6403fb 100644 --- a/src/app_model/types/_keys/_keybindings.py +++ b/src/app_model/types/_keys/_keybindings.py @@ -302,17 +302,18 @@ def validate(cls, v: Any) -> "KeyBinding": raise TypeError("invalid keybinding") # pragma: no cover -_re_ctrl = re.compile(r"(ctrl|control|ctl|⌃|\^)[\+|\-]") -_re_shift = re.compile(r"(shift|⇧)[\+|\-]") -_re_alt = re.compile(r"(alt|opt|option|⌥)[\+|\-]") -_re_meta = re.compile(r"(meta|super|win|windows|⊞|cmd|command|⌘)[\+|\-]") +_re_ctrl = re.compile(r"(ctrl|control|ctl|\^)[\+|\-]|⌃[\+|\-]?(?=.)") +_re_shift = re.compile(r"shift[\+|\-]|⇧[\+|\-]?(?=.)") +_re_alt = re.compile(r"(alt|opt|option)[\+|\-]|⌥[\+|\-]?(?=.)") +_re_meta = re.compile(r"(meta|super|win|windows|cmd|command)[\+|\-]|[⊞⌘][\+|\-]?(?=.)") def _parse_modifiers(input: str) -> tuple[dict[str, bool], str]: """Parse modifiers from a string (case insensitive). modifiers must start at the beginning of the string, and be separated by - "+" or "-". e.g. "ctrl+shift+alt+K" or "Ctrl-Cmd-K" + "+" or "-". e.g. "ctrl+shift+alt+K" or "Ctrl-Cmd-K". Modifier symbols + may omit the separator, as long as a key follows. e.g. "⌘⇧K" """ remainder = input.lower() diff --git a/tests/test_keybindings.py b/tests/test_keybindings.py index 64a61c39..7ef94700 100644 --- a/tests/test_keybindings.py +++ b/tests/test_keybindings.py @@ -203,3 +203,16 @@ def test_int_conversion_respects_os(enc: int, os: OperatingSystem, expect: str) kb = KeyBinding.from_int(enc, os=os) assert kb.to_text(os=os) == expect assert kb.to_int(os=os) == enc + + +@pytest.mark.parametrize( + ("text", "expected"), + [ + ("Ctrl+↵", "Ctrl+Enter"), + ("⌘⇧↵", "Shift+Meta+Enter"), + ("⌘", "Meta"), + ("Option", "Alt"), + ], +) +def test_from_str_symbols(text: str, expected: str) -> None: + assert str(KeyBinding.from_str(text)) == expected